A very readable way to do this ...

fin = open("File1.txt", "r")
data1 = fin.read()
fin.close()

fin = open("File2.txt", "r")
data2 = fin.read()
fin.close()

combined_data = data1 + data2

fout = open("File1&2.txt", "w")
fout.write(combined_data)
fout.close()

This can of course be simplified.

Or you can simply append ...

# append file2 data to file1 data

fin = open("File2.txt", "r")
data2 = fin.read()
fin.close()

fout = open("File1.txt", "a")
fout.write(data2)
fout.close()

Edited 6 Years Ago by vegaseat: append

hi can anybody tell me what does the following code mean?Thank you

import sys
print >> sys.stderr, 'Fatal error: invalid input!'

logfile = open('/tmp/mylog.txt', 'a')
print >> logfile, 'Fatal error: invalid input!'
logfile.close()

Editor's note:
Please don't hijack someone else's unrelated thread, start your own thread with the proper title.

Edited 6 Years Ago by vegaseat: n/a

Use code tags, Makes it easier for us to read.

Let me explain the code:

[B]import[/B] sys
[B]print [/B]>> sys.stderr, 'Fatal error: invalid input!'

logfile = open('/tmp/mylog.txt', 'a')
[B]print[/B] >> logfile, 'Fatal error: invalid input!'
logfile.close()
  1. We import the sys module documentation here
  2. We print the message 'Fatal error... etc' to the system's standard error pipe

  3. Open a file located at /tmp/mylog.txt in 'append' mode, which means open it for writing but leave the existing contents intact (alternately could say 'w' for 'write' mode, which clears the contents when it opens the file)

  4. Print the message into the logfile
  5. Close the logfile.

Edited 3 Years Ago by happygeek: fixed formatting

This question has already been answered. Start a new discussion instead.