4
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by eva yang
0

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 by vegaseat: append

-1

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 by vegaseat: n/a

0

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 by happygeek: fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.