首页 > 代码库 > python分割文件

python分割文件

#将一个文件分割成固定行的小文件

import os import sys num = 0 fnum = 0 path = sys.argv[1] fname = ‘data-%d‘ % fnum fw = open(fname,‘w‘) for t in os.listdir(path): filename = os.path.join(path,t) fd = open(filename) for line in fd: line = line.strip() num = num + 1 if line: if num > 10000000: num = 0 fw.close() fnum = fnum + 1 fname = ‘data-%d‘ % fnum fw = open(fname,‘w‘) fw.write(line + ‘\n‘) fw.close()

 

python分割文件