标签:
1 """ 2 this is aa customizable version of the standard unix split command-line 3 utility;because it is written in python,it also works on windows and can be 4 easily modifyed;because it export a function,its logic can also be imported 5 and resued in other applications 6 """ 7 import sys,os 8 kilobytes =1024 9 megabytes = kilobytes*1000 10 chunksize = int(1.4* megabytes) #default roughtly a floppy 11 12 def split(fromfile,todir,chunksize=chunksize): 13 if not os.path.exists(todir): # caller handlers errors 14 os.mkdir(todir) #make dir,read/write parts 15 else: 16 for fname in os.listdir(todir): 17 os.remove(os.path.join(todir,fname)) #delete any exiting files 18 partnum =0 19 input = open(fromfile,‘rb‘) 20 while True: 21 chunk = input.read(chunksize) 22 if not chunk:break 23 partnum +=1 24 filename = os.path.join(todir,(‘part%04d‘ % partnum)) 25 fileobj = open(filename,‘wb‘) 26 fileobj.write(chunk) 27 fileobj.close() 28 input.close() 29 assert partnum<=9999 30 return partnum 31 32 if __name__ ==‘__main__‘: 33 if len(sys.argv) == 2 and sys.argv[1]== ‘-help‘: 34 print(‘use:split.py [file to split target-dir [chunksize]]‘) 35 else: 36 if len(sys.argv) <3: 37 interactive =True 38 fromfile =input(‘File to be split?‘) 39 todir = input(‘directory to store part files?‘) 40 else: 41 interactive = False 42 fromfile,todir = sys.argv[1:3] 43 if len(sys.argv) == 4:chunksize =int(sys.argv[3]) 44 absfrom,absto = map(os.path.abspath,[fromfile,todir]) 45 print(‘splitting‘,absfrom,‘to‘,absto,‘by‘,chunksize) 46 try: 47 parts = split(fromfile,todir,chunksize) 48 except: 49 print(‘error during split:‘) 50 print(sys.exc_info()[0],sys.exc_info()[1]) 51 else: 52 print(‘split finished:‘,parts,‘parts are in ‘,absto) 53 if interactive: 54 input(‘press enter key‘) #pause if clicked 55
split to 200k
标签:
原文地址:http://www.cnblogs.com/lxk613/p/4827677.html