[Tutor] Writing to a file...

Bartz, Gregory H. Gregory.H.Bartz at usa-spaceops.com
Tue Sep 14 21:26:27 CEST 2004


I'm somewhat new to Python and this is my first question to the list.

In the example below you can see that it takes four write statements to equal one print statement.
At the moment I'm using print statements and redirecting them to a file when I run the script (foo.py > outfile).


Example using print:

dimes = 3
nickels = 5
numofcoins = dimes + nickels
money = dimes * 0.1 + nickels * 0.5
print 'You have ', numofcoins, 'coins totaling $', money

>>>You have 8 coins totaling $ 0.55

for the same line, write() is more cumbersome:

outfile.write('You have ')
outfile.write(numofcoins)
outfile.write(' coins totaling $')
outfile.write(money)

Is there a more efficient way of doing this, or some way I can redirect the output internally?

-Greg Bartz


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040914/024ad7a9/attachment.htm


More information about the Tutor mailing list