How to merge multiple files?
<p>My files are in txt format and I wrote a short code to merge all three into a single one. 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. 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?</p>

<pre><code>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)
</code></pre>

<p>Simply use <a href="https://docs.python.org/3/library/fileinput.html" rel="nofollow"><code>fileinput</code></a> from the standard library:</p>

<pre><code>import fileinput

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

<p>If you ever need finer control over memory use or need to handle binaries, use <code>shutil.copyfileobj</code>:</p>

<pre><code>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)
</code></pre>

python python-3.x