1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import sys,os
kilobytes = 1024 megabytes = kilobytes*1000 chunksize = int(200*megabytes)
def getPartSum(fromfile,chunksize): ''' get the total number of part ''' if os.path.getsize(fromfile)%chunksize != 0: return int(os.path.getsize(fromfile)/chunksize)+1 else: return int(os.path.getsize(fromfile)/chunksize)
def split(fromfile,todir,chunksize=chunksize): ''' split files by the chunksize ''' if not os.path.exists(todir): os.mkdir(todir) else: for fname in os.listdir(todir): os.remove(os.path.join(todir,fname)) partnum = 0 partsum = getPartSum(fromfile,chunksize) inputfile = open(fromfile,'rb') while True: chunk = inputfile.read(chunksize) if not chunk: break partnum += 1 filename = os.path.join(todir,('part%04d'%partnum)) fileobj = open(filename,'wb') fileobj.write(bytes.fromhex('%04x'%partnum)) fileobj.write(bytes.fromhex('%04x'%partsum)) fileobj.write(chunk) fileobj.close() return partnum if __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)
|