saving output to file

Tyler Eaves tyler at cg1.org
Mon Apr 28 21:15:28 EDT 2003


Eric Anechiarico wrote:

> I am still not as proficient as I would like to be in Python, so this may
> be a trivial question to most.
> 
> I want to have the output I get in my IDE window (using MacPython 2.2.2
> primarily in Carbon) to be written to a file instead of having to copy the
> output and then paste it to a text file myself.  How would I go about
> doing this?
> 
> Mainly I would be using the code to do this with programs that calculate
> outputs of large numbers (primes, Fibonacci, etc.) and they sometimes
> overflow the buffer and I cannot get back very far to view the beginning.
> Make sense?  Basically since I am doing this in the IDE window anyway.
> 
> Any suggestions?
> 
> Thanks!

import sys
outf = open('somefile.txt','w')
sys.stdout = outf

Leave the rest of your program unchagned.

Has the advantage that you can easily change the behavior back and forth by
commenting out the sys.stdout = line.







More information about the Python-list mailing list