2

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?

Community
  • 1
  • 1
dylan
  • 51
  • 1
  • 1
  • 4
  • 2
    How are they "labelled"? Can you post your code? – Blender Dec 10 '12 at 04:12
  • Learn to ask reasonable questions. –  Dec 10 '12 at 04:21
  • No need to flame. I definitely tried searching for this question and couldn't find it. I'm not an idiot. Yes, I could have worded my question using Python vernacular, but it didn't occur to me at that time. Other people found my question to be quite clear. By "labelled" I mean anything that looks like: print 'Hello world!' I would want Hello World! to be in the text document. The code is just under 1000 lines. Do your really want me to post it? – dylan Dec 10 '12 at 04:33
  • 1
    @dylan: People don't want you to post 1000's of lines of code, they want you to meet them halfway by creating a short code sample that demonstrates the problem. This actually has benefits for you as well, since in trying to create a sample that demonstrates the problem, you might actually solve it yourself, and you might be more likely to get an answer because it's easier for people to quickly try out solutions if they have some runnable code to start with. – Marius Dec 10 '12 at 04:50
  • Hey Blender, I sent you an email. And Marius, I know exactly what you mean. But it appears to me that the people who posted "things???", "duplicate", and "learn to ask reasonable questions", clearly weren't concerned about the ease of the solution or me trying to solve it myself by retyping it. I think you see it too. – dylan Dec 10 '12 at 05:29
  • Being one of those who commented, I think the fact that I took the time to answer your question backs up my intention. My problem with the question is that while you understand what you are doing with your own code, it appears ambiguous to others. A common problem on StackOverflow is that new comers will ask a very short question with no examples, leaving a wide gap in potential answers. What the community wants to see (hence the unfortunate downvotes and close requests) is: 1) A clear question to a problem that can be solved – jdi Dec 10 '12 at 05:38
  • 2) what you expected the results to be 3) what results you are getting from the attempts you have made. This question only covers half of #1. I happened to see some answers and came to understand what you were after, and submitted an answer for myself. – jdi Dec 10 '12 at 05:39

3 Answers3

7

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.

jdi
  • 90,542
  • 19
  • 167
  • 203
6

Redirect stdout to a file.

import sys
sys.stdout = open('log.txt', 'w')
print 'Write this to file.'

EDIT: if you are using this method, still close the file when you are done writing: sys.stdout.close()

Community
  • 1
  • 1
Aesthete
  • 18,622
  • 6
  • 36
  • 45
2

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()
Diego Basch
  • 12,764
  • 2
  • 29
  • 24