大文件不断行分割

来源:互联网 发布:历峰集团 知乎 编辑:程序博客网 时间:2024/04/29 15:51
#! /usr/bin/python#! -*- coding:utf-8 -*-import osdef SplitFile(cnt):  path='e:\data'  filename=path+'\\qunti_deal.txt'  os.chdir(path)  i=0  n=0  size=os.path.getsize(filename)/1024/cnt  print size  temp = open(filename+'.part'+str(i),'w')  f=open(filename,'r')  while True:    buf = f.read(1024)    if buf=='':      print filename+'.part'+str(i)+';'      temp.close()      f.close()      return    n+=1    if n==size:      t=buf.rfind('\n')+1      temp.write(buf[:t])      n=0      print filename+'.part'+str(i)+';'      i+=1      temp.close()      temp=open(filename+'.part'+str(i),'w')      temp.write(buf[t:])      continue    temp.write(buf)if __name__=='__main__':SplitFile(int(raw_input('#files:')))

1 0