我的文件是txt格式,我写了一个简短的代码,将所有三个合并为一个。输入文件是(1)18.8MB,超过16K列,(2)18.8MB,超过16K列,(3)10.5MB,超过7K列。代码有效,但它只合并前两个文件并创建输出文件。不包括第三个输入文件中的数据。这里有什么问题,是否有关于txt文件大小的限制?
filenames = ['/Users/icalic/Desktop/chr_1/out_chr1_firstset.txt', '/Users/icalic/Desktop/chr_1/out_chr1_secondset.txt', '/Users/icalic/Desktop/chr_1/out_chr1_thirdset.txt']
with open('/Users/icalic/Desktop/chr1_allfinal.txt', 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
for line in infile:
outfile.write(line)
答案 0 :(得分:2)
只需使用标准库中的fileinput
:
import fileinput
filenames = [ '...' ]
with open(output_file, 'w') as file_out, fileinput.input(filenames) as file_in:
file_out.writelines(file_in)
如果您需要更好地控制内存使用或需要处理二进制文件,请使用shutil.copyfileobj
:
filenames = [ '...' ]
buffer_length = 1024*1024*10 # 10 MB
with open('output_file.txt', 'wb') as out_file:
for filename in filenames:
with open(filename, 'rb') as in_file:
shutil.copyfileobj(in_file, out_file, buffer_length)