[Tutor] Print to file

Phil phil_lor at bigpond.com
Mon Mar 11 09:24:09 CET 2013


The usage of print to file is much like the Arduino print to serial 
device and it's got me curious to know what the correct syntax is. 
Neither of the following is correct, but the second one seems closer to 
the mark.

 >>> print("test", file="/home/phil/Python/words")
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'write'

 >>> f = open("/home/phil/Python/words", 'w')
 >>> print("test", file=f

I've used the "with" statement to write and read files but print to file 
could be handy some time.

-- 
Regards,
Phil


More information about the Tutor mailing list