Newbie: print-to-file-Syntax

Stefan Schwarzer s.schwarzer at ndh.net
Sat May 18 16:35:42 EDT 2002


Hello Sabine

Sabine Richter wrote:
> I looked the syntax up in the python doc, but nevertheless, I don't
> understand it. What's wrong?
> 
> PROT=open("c:\\existing_dir\\prot.txt",'w')
> PROT.write("Written String")
> print >> PROT "Printed String"
> print PROT "Printed String"
> 
> PROT.close()

others have pointed out that you need to add a comma after the name of
the file(-like) object.

Off-topic-ly, I just wanted to say, that you can use forward slashes
in the `open` function, even on Windows:

PROT = open("c:/existing_dir/prot.txt", 'w')

That's a bit easier to write. :)

Stefan



More information about the Python-list mailing list