[Tutor] How to print to a file?
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Wed Feb 18 16:16:37 EST 2004
On Wed, 18 Feb 2004, denny wrote:
> I want to write something to a file like this:
> print >>filehandle, "....%d...%s..."%(num,string)
> but it raise error
Hi Denny,
What kind of error? Can you show us exactly what the error message looks
like? As far as I can tell, the code that you've showed us,
print >>filehandle, "....%d...%s..." % (num,string)
looks syntactically correct to me. We really need to see why Python
doesn't seem to like it; show us the error message, verbatim, and we can
go from there.
> Can someone tell me how can I print it above to a file just like we
> print to the stdout(print "....%d...%s..."(num,string)). Or is there
> some funtion as we use in C/C++( fprint etc)
The method you tried above should work.
Alternatively, you can use the 'write' method of a file object. For
example, since 'sys.stdout' is a file object, we can say something like:
###
sys.stdout.write('hello world\n')
###
Talk to you later!
More information about the Tutor
mailing list