大文件不断行切割

#! /usr/bin/python
#! -*- coding:utf-8 -*-
import os
def 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:')))

你可能感兴趣的:(大文件)