日志文件分割器

xiaoxiao2021-02-27  125

import sys,os,re kilobytes = 1024 megabytes = kilobytes*1024 chunksize = int(10*megabytes) def split(fromfile,todir,chunksize): print(fromfile,todir,chunksize) if not os.path.exists(todir):#check todir exsits or not os.mkdir(todir) else: for fname in os.listdir(todir): os.remove(os.path.join(todir,fname)) index=0 inputfile = open(fromfile,'rb')# while True: chunk = inputfile.read(chunksize) if not chunk:             #check the chunk is empty break index += 1 filename = os.path.join(todir,('part'+str(index)+"."+fromfile.split('.')[-1])) fw = open(filename,'w') fw.write(str(chunk))         #write data into partfile fw.close() inputfile.close()

return index

if __name__=='__main__': fromfile  = input('File to be split?'+"\n input:") todir     = input('Directory to store part files?'+"\n input:") n = int(input('Chunksize to be split mb?'+"\n input:")) chunksize = int(n*megabytes) absfrom,absto = map(os.path.abspath,[fromfile,todir]) print('Splitting',absfrom,'to',absto,'by',chunksize) try: index = split(fromfile,todir,chunksize) except: print('Error during split:') print(sys.exc_info()[0],sys.exc_info()[1]) else: print('split finished:',index,'parts are in',absto)
转载请注明原文地址: https://www.6miu.com/read-15922.html

最新回复(0)