在平常的生活中,我们会遇到下面这样的情况:
你下载了一个比较大型的游戏(假设有10G),现在想跟你的同学一起玩,你需要把这个游戏拷贝给他。
然后现在有一个问题是文件太大(我们不考虑你有移动硬盘什么的情况),假设现在只有一个2G或4G的优盘,该怎么办呢?
有很多方法,例如winrar压缩的时候分成很多小卷,这里不累述。
在学习python之后,我们自己就可以解决这个问题啦。
我们可以自己写一个脚本去分割合并文件,将文件分割成适合优盘大小的小文件,在拷贝,然后再合并。
import sys,os
kilobytes= 1024megabytes= kilobytes*1000chunksize= int(200*megabytes)#defaultchunksize
def split(fromfile,todir,chunksize=chunksize):ifnot os.path.exists(todir):#check whether todir exists or not
os.mkdir(todir)else:for fname inos.listdir(todir):
os.remove(os.path.join(todir,fname))
partnum= 0inputfile= open(fromfile,'rb')#open the fromfilewhileTrue:
chunk=inputfile.read(chunksize)if not chunk: #check the chunk isemptybreakpartnum+= 1filename= os.path.join(todir,('part%04d'%partnum))
fileobj= open(filename,'wb')#make partfile
fileobj.write(chunk) #write data into partfile
fileobj.close()returnpartnumif __name__=='__main__':
fromfile= input('File to be split?')
todir= input('Directory to store part files?')
chunksize= int(input('Chunksize to be split?'))
absfrom,absto=map(os.path.abspath,[fromfile,todir])
print('Splitting',absfrom,'to',absto,'by',chunksize)try:
parts=split(fromfile,todir,chunksize)
except:
print('Error during split:')
print(sys.exc_info()[0],sys.exc_info()[1])else:
print('split finished:',parts,'parts are in',absto)
下面是脚本运行的例子:
我们在F有一个X—MEN1.rar文件,1.26G大小,我们现在把它分割成400000000bit(大约380M)的文件。
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64bit (AMD64)] on win32
Type"copyright", "credits" or "license()" formore information.>>> ================================ RESTART ================================
>>>File to be split?F:\X-MEN1.rar
Directory to store part files?F:\split
Chunksize to be split?400000000Splitting F:\X-MEN1.rar to F:\split by 400000000split finished:4 parts are inF:\split>>>
这是分割后的文件:
下面是文件合并脚本:
import sys,os
def joinfile(fromdir,filename,todir):ifnot os.path.exists(todir):
os.mkdir(todir)ifnot os.path.exists(fromdir):
print('Wrong directory')
outfile= open(os.path.join(todir,filename),'wb')
files= os.listdir(fromdir) #list all the part files inthe directory
files.sort() #sort part files to readinorderfor file infiles:
filepath=os.path.join(fromdir,file)
infile= open(filepath,'rb')
data=infile.read()
outfile.write(data)
infile.close()
outfile.close()if __name__=='__main__':
fromdir= input('Directory containing part files?')
filename= input('Name of file to be recreated?')
todir= input('Directory to store recreated file?')try:
joinfile(fromdir,filename,todir)
except:
print('Error joining files:')
print(sys.exc_info()[0],sys.exc_info()[1])
运行合并脚本,将上面分割脚本分割的文件重组:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64bit (AMD64)] on win32
Type"copyright", "credits" or "license()" formore information.>>> ================================ RESTART ================================
>>>Directory containing part files?F:\split
Name of file to be recreated?xman1.rar
Directory to store recreated file?F:\>>>
运行之后可以看到F盘下生成了重组的xman.rar