Hi all,

How to write one file to the end of other file using python.

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()

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.

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.

thanks! I got it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.