用Python进行文件操作是比较简单的,在Python中file是内置类型之一,内置的函数open、file都可以创建file对象,创建好之后就可以对其进行读写等操作。
近几天看Python Programing 3rd ,觉得书很不错。
文件分割的原理很简单:以二进制形式打开文件流,按照指定的大小读取,然后写入新文件。
文件合并的原理正好相反。
下面的代码摘自PP3rd里面。
split_file.py
近几天看Python Programing 3rd ,觉得书很不错。
文件分割的原理很简单:以二进制形式打开文件流,按照指定的大小读取,然后写入新文件。
文件合并的原理正好相反。
下面的代码摘自PP3rd里面。
split_file.py
#
!/usr/bin/python
# #########################################################################
# split a file into a set of parts; join.py puts them back together;
# this is a customizable version of the standard unix split command-line
# utility; because it is written in Python, it also works on Windows and
# can be easily modified; because it exports a function, its logic can
# also be imported and reused in other applications;
# #########################################################################
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int( 1.4 * megabytes) # default: roughly a floppy
def split(fromfile, todir, chunksize = chunksize):
if not os.path.exists(todir): # caller handles errors
os.mkdir(todir) # make dir, read/write parts
else :
for fname in os.listdir(todir): # delete any existing files
os.remove(os.path.join(todir, fname))
partnum = 0
input = open(fromfile, ' rb
# #########################################################################
# split a file into a set of parts; join.py puts them back together;
# this is a customizable version of the standard unix split command-line
# utility; because it is written in Python, it also works on Windows and
# can be easily modified; because it exports a function, its logic can
# also be imported and reused in other applications;
# #########################################################################
import sys, os
kilobytes = 1024
megabytes = kilobytes * 1000
chunksize = int( 1.4 * megabytes) # default: roughly a floppy
def split(fromfile, todir, chunksize = chunksize):
if not os.path.exists(todir): # caller handles errors
os.mkdir(todir) # make dir, read/write parts
else :
for fname in os.listdir(todir): # delete any existing files
os.remove(os.path.join(todir, fname))
partnum = 0
input = open(fromfile, ' rb