import os
import time
def mkSubFile(lines, srcName, sub):
[des_filename, extname] = os.path.splitext(srcName)
filename = des_filename + '_' + str(sub) + extname
print 'make file: %s' % filename
fout = open(filename, 'w')
try:
fout.writelines(lines)
return sub + 1
finally:
fout.close()
def splitByLineCount(filename, count):
with open(filename, 'rb') as fin:
try:
head = fin.readline()
buf = []
sub = 1
buf.append(head)
for line in fin:
buf.append(line)
if len(buf) == count:
sub = mkSubFile(buf, filename, sub)
buf = []
if len(buf) != 0:
sub = mkSubFile(buf, filename, sub)
finally:
fin.close()
if __name__ == '__main__':
# 每个文件存储40000行数据
begin = time.time()
splitByLineCount('/home/python/Desktop/sum.txt', 40000)
end = time.time()
print('time is %d seconds ' % (end - begin))