File I/O
File input and output are done using file objects. open() returns a new file object. readline() reads one line at a time from the file including the terminating newline character. write() writes raw data to the file. You can also use the print statement to redirect output to a file instead of the terminal. Don’t forget to close() your files when you’re done with them.
[code lang=”python”]
#file i/o
speech = open(”lincoln.txt”)
outfile = open(”output.txt”, “w”)
line = speech.readline()
while line:
print line
line = speech.readline()
speech.close()
out = “done”
outfile.write(out)
outfile.close()
[/code]