python有效地将文件分割成更小的块
在python中,如何有效地将文件分割成更小的块? 例如,我有一个包含140行的文件。我想将文件拆分为file1.txt、file2.txt、file3.txt。文件1有50行,文件2有50行,文件3有其余40行python有效地将文件分割成更小的块,python,Python,在python中,如何有效地将文件分割成更小的块? 例如,我有一个包含140行的文件。我想将文件拆分为file1.txt、file2.txt、file3.txt。文件1有50行,文件2有50行,文件3有其余40行 chunksize = 50 fid = 1 with open('path/to/file') as infile: f = open('file%d.txt' %fid, 'w') for i,line in enumerate(infile): f
chunksize = 50
fid = 1
with open('path/to/file') as infile:
f = open('file%d.txt' %fid, 'w')
for i,line in enumerate(infile):
f.write(line)
if not i%chunksize:
f.close()
fid += 1
f = open('file%d.txt' %fid, 'w')
f.close()