在平常的生活中,我們會遇到下面這樣的情況:
你下載了一個比較大型的游戲(假設有10G),現在想跟你的同學一起玩,你需要把這個游戲拷貝給他。
然后現在有一個問題是文件太大(我們不考慮你有移動硬盤什么的情況),假設現在只有一個2G或4G的優盤,該怎么辦呢?
有很多方法,例如winrar壓縮的時候分成很多小卷,這里不累述。
在學習python之后,我們自己就可以解決這個問題啦。
我們可以自己寫一個腳本去分割合並文件,將文件分割成適合優盤大小的小文件,在拷貝,然后再合並。
下面是文件分割腳本:
1 import sys,os 2 3 kilobytes = 1024 4 megabytes = kilobytes*1000 5 chunksize = int(200*megabytes)#default chunksize 6 7 def split(fromfile,todir,chunksize=chunksize): 8 if not os.path.exists(todir):#check whether todir exists or not 9 os.mkdir(todir) 10 else: 11 for fname in os.listdir(todir): 12 os.remove(os.path.join(todir,fname)) 13 partnum = 0 14 inputfile = open(fromfile,'rb')#open the fromfile 15 while True: 16 chunk = inputfile.read(chunksize) 17 if not chunk: #check the chunk is empty 18 break 19 partnum += 1 20 filename = os.path.join(todir,('part%04d'%partnum)) 21 fileobj = open(filename,'wb')#make partfile 22 fileobj.write(chunk) #write data into partfile 23 fileobj.close() 24 return partnum 25 if __name__=='__main__': 26 fromfile = input('File to be split?') 27 todir = input('Directory to store part files?') 28 chunksize = int(input('Chunksize to be split?')) 29 absfrom,absto = map(os.path.abspath,[fromfile,todir]) 30 print('Splitting',absfrom,'to',absto,'by',chunksize) 31 try: 32 parts = split(fromfile,todir,chunksize) 33 except: 34 print('Error during split:') 35 print(sys.exc_info()[0],sys.exc_info()[1]) 36 else: 37 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 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> File to be split?F:\X-MEN1.rar Directory to store part files?F:\split Chunksize to be split?400000000 Splitting F:\X-MEN1.rar to F:\split by 400000000 split finished: 4 parts are in F:\split >>>
這是分割后的文件:
下面是文件合並腳本:
1 import sys,os 2 3 def joinfile(fromdir,filename,todir): 4 if not os.path.exists(todir): 5 os.mkdir(todir) 6 if not os.path.exists(fromdir): 7 print('Wrong directory') 8 outfile = open(os.path.join(todir,filename),'wb') 9 files = os.listdir(fromdir) #list all the part files in the directory 10 files.sort() #sort part files to read in order 11 for file in files: 12 filepath = os.path.join(fromdir,file) 13 infile = open(filepath,'rb') 14 data = infile.read() 15 outfile.write(data) 16 infile.close() 17 outfile.close() 18 if __name__=='__main__': 19 fromdir = input('Directory containing part files?') 20 filename = input('Name of file to be recreated?') 21 todir = input('Directory to store recreated file?') 22 23 try: 24 joinfile(fromdir,filename,todir) 25 except: 26 print('Error joining files:') 27 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 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more 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