Newbie: print-to-file-Syntax

Brian Quinlan brian at sweetapp.com
Sat May 18 15:07:34 EDT 2002


> PROT=open("c:\\existing_dir\\prot.txt",'w')
> PROT.write("Written String")
> print >> PROT "Printed String"
> print PROT "Printed String"
> 
> PROT.close()

The correct syntax is:

print >> PROT, "Printed String"
 
> And a second question:
> 
> Is it possible to write:
> PROT=None
> print... print-to-file-syntax
> 
> Later, I want to print to file, but now I want to print to STDOUT.
> The doc says: If the first expression evaluates to None, then
sys.stdout
> is used as the file for output.

If the docs say you can do it, you probably can :-) I would probably
write it as:

import sys
PROT = sys.stdout

Cheers,
Brian






More information about the Python-list mailing list