How do I combine two files without overwriting any data in python?
Question: I need to concatenate two files, one which contains a single number and the
other which contains at least two rows of data. I have tried
shutil.copyfile(file2,file1) and subprocess.call("cat " + file2 + " >> " +
file1, shell=True), both things give me the same result. The file with the
single number contains an integer and a newline (i.e. two characters) so when
I bring the two files together the first two characters of file2 are
overwritten instead of just added to the end. If I do it through the shell
using "cat file2 >> file1" this does not happen and it works perfectly.

Here is what I mean:

    
    
    import numpy as np
    from subprocess import call
    
    f.open(file1)
    f.write('2\n')
    np.savetxt(file2,datafile,fmt)
    call("cat " + file2 " >> " + file1, shell=True)
    

So instead of getting:

    
    
    2
    data data data ...
    data data data ...
    

I get:

    
    
    2
    ta data data ...
    data data data ...
    

I have no idea what is causing this issue but it is very frustrating. Any
suggestions?


Answer: Have you tried closing `file1` first?

    
    
     f.close()
     np.savetxt... Etc
    

