Cannot merge .txt files having different encoding in Python
Question: I'm trying to merge a number of .txt files into one file. Some of the files
are plain text .txt files I created in advance, while some are files written
by the script itself. The script successfully merges some of the files (the
ones I wrote in advance) but does not include the ones created by the script,
even though it raises no error. It literally seems to ignore their existence.

I have tried different methods, including `shutil` and `fileinput`, and even
using `cat` from `subprocess.call` but without luck.

From the terminal, the files I created in advance are "XML document text"
(though I actually saved it as plain text, but it is formatted as XML), while
the other ones are "ASCII text". I believe this is the problem, as the XML
ones were .rtf files, and they wouldn't merge before I converted them – only
the first of the list would be included in the output file.

    
    
    import os, itertools, subprocess, fileinput, shutil
    
    os.chdir('/Users/MicTonutti/Dropbox/MRes/Individual Project/FEBio/Simulation')
    
    forces = itertools.permutations([-1.5,-1,-0.5,0],3)
    forces = list(forces)
    force_node = 174
    
    for i in range(0,len(forces)):
        filename = '0_Insert' + str(i) + '.txt'
        f = open(filename, 'w')
        string_x = '<nodal_load bc="x" lc="2"> <node id="' + str(force_node) + '">' + str(forces[i][1]) + '</node> </nodal_load>\n'
        string_y = '<nodal_load bc="y" lc="2"> <node id="' + str(force_node) + '">' + str(forces[i][2]) + '</node> </nodal_load>\n'
        string_z = '<nodal_load bc="z" lc="2"> <node id="' + str(force_node) + '">' + str(forces[i][2]) + '</node> </nodal_load>\n'
        f.write(string_x + string_y + string_z)
    
    filename_2 = '1_Insert' + str(i) + '.txt'
    g = open(filename_2, 'w')
    string_1 = '<logfile>\n <node_data data="x;y;z" file = "coord_data' + str(i) + '.txt" delim=", "> </node_data>\n'
    string_2 = '<node_data data="ux;uy;uz" file = "displacement_data' + str(i) + '.txt" delim=", "> </node_data>\n </logfile>\n'
    g.write(string_1 + string_2)
    
    files_list = ['Simulation 1.txt', filename, 'Simulation 2.txt', filename_2, 'Simulation 3.txt']
    output_file = '/Users/MicTonutti/Dropbox/MRes/Individual Project/FEBio/Simulation/Python Output/FEBio Simulation Output' + str(i) + '.txt'
    
    with open(output_file, 'w') as outfile:
        for infile in files_list:
            shutil.copyfileobj(open(infile), outfile)
    

The output basically looks like this:

Text of "Simulation1.txt" + Text of "Simulation2.txt + Text of
"Simulation3.txt", without the files in the middle. Any ideas?

Thanks.


Answer: You should call `flush()` on the files you write before you try to read them
again, else the written data might still be buffered.

Apart from that I wanted to point out that you write to 24 different
`0_Insert` files in your loop (`0_Insert1.txt` to `0_Insert23.txt`), but later
on only read from the last one. Is this what you actually are trying to do? Or
should your loop also include the bottom part of your code?

