Possible Duplicate:
Redirect stdout to a file in Python?
I've finally finished all the code for my project. Now I need all of the things that are labelled "print" to actually be exported into a .txt document. How would I go about doing that?
Possible Duplicate:
Redirect stdout to a file in Python?
I've finally finished all the code for my project. Now I need all of the things that are labelled "print" to actually be exported into a .txt document. How would I go about doing that?
I would not even approach this from a python-fix perspective, but simply redirect the output of running your python script:
python /path/to/script/myscript.py > /path/to/output/myfile.txt
Nothing has to change in your script, and all print statements will end up in your text file.
1) open a file for writing:
f = open("file.txt", "w")
2) replace all your print statements by print >>f, for example:
print "hello"
becomes print >>f, "hello
3) close the file when you're done
f.close()