简体   繁体   English

如何合并多个文件?

[英]How to merge multiple files?

My files are in txt format and I wrote a short code to merge all three into a single one. 我的文件是txt格式,我写了一个简短的代码将这三个文件合并为一个文件。 Input files are (1) 18.8MB with over 16K columns, (2) 18.8MB with over 16K columns and (3) 10.5MB with over 7K columns. 输入文件为(1)18.8MB,超过16K列;(2)18.8MB,超过16K列;(3)10.5MB,超过7K列。 The code works, however it only merges first two files and creates output file. 该代码有效,但是仅合并前两个文件并创建输出文件。 The data from the third input file is not included. 不包括来自第三个输入文件的数据。 What is wrong here and is there any limit regarding the size for txt files? 这有什么问题,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)

Simply use fileinput from the standard library: 只需使用标准库中的fileinput

import fileinput

filenames = [ '...' ]
with open(output_file, 'w') as file_out, fileinput.input(filenames) as file_in:
    file_out.writelines(file_in)

If you ever need finer control over memory use or need to handle binaries, use shutil.copyfileobj : 如果您需要更好地控制内存使用或需要处理二进制文件,请使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM