"Write to a file" question please?

Stephen Hansen stephen at cerebralmaelstrom.com
Wed Jul 5 13:45:27 EDT 2000


I *think*...
Print is a statement that lets you print out multiple things using the
comma, the 'write' function does not work the same way. It takes a string
argument and writes it out to the file.

To accomplish what you want, you can use the module operator (there's other
ways too, but its my favorite :))

number = 100
print "Number = %s" % number
OUT = open("TT.txt",'w')
OUT.write("Number = %s" % number)
OUT.close()

If you have more then one number you're writing out, make the right-operand
of the modulo operator a tuple.. E.g.

num1 = 100
num2 = 500
print "Number 1 = %s and Number 2 = %s" % (num1,num2)

See? :)

--Stephen



<cmfinlay at SPAMmagnet.com.au> wrote in message
news:tzFjOfN2zOzPHwvl3ncklEAEb+fa at 4ax.com...
> number = 100
> print "Number =", number
> OUT = open("TT.txt","w")
> OUT.write("Number =", number) # Error
> OUT.close()
>
> The code above gives the error.
>
> Traceback (innermost last):
>   File "ttest.py", line 5, in ?
>     OUT.write("Number =", number)
> TypeError: read-only buffer, tuple
>
> OUT.write("Hello World")
>
> Works but a number or variable of a number does not.
>
> I have been reading documents on Python to no avail this time.
> I am using Win98 with the latest python for this OS.
>
> Any help would be much appreciated
> E-mail me at cmfinlaySPAM at magnet.com.au
> or reply here.





More information about the Python-list mailing list