Writing an integer to a file?!?

Grant Edwards grante at visi.com
Tue Nov 7 14:07:39 EST 2000


In article <fFXN5.4753$jv2.548430 at newsc.telia.net>, Fredrik Lundh wrote:

>Gabriel Ambuehl wrote:
>> I get the following error:
>> >>> f.write(int(time.time()))
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> TypeError: read-only character buffer, int
>
>the error message means "expected a character buffer,
>got an integer".  to put it another way, "write" needs a
>string, not an integer.

That message could be a bit more verbose.  ;)

>try:
>
>    f.write(str(int(time.time())))
>
>or perhaps:
>
>    f.write("%d" % time.time())

If you _really_ want to write binary data rather than a string
(generally a bad idea), then you need to use the 'struct'
module to convert the int to a "string" containing binary data.

[Insert standard warnings about byte ordering, etc.]

-- 
Grant Edwards                   grante             Yow!  NOW do I get to blow
                                  at               out the CANLDES??
                               visi.com            



More information about the Python-list mailing list