Writing an integer to a file?!?

Fredrik Lundh fredrik at effbot.org
Tue Nov 7 20:30:11 CET 2000


Gabriel Ambuehl wrote:
> >     f.write("%d" % time.time())
> 
> One sole problem with this (had tested it before):
> f.read() on the timestamp file will not return the
> last digit thus making the whole thing pretty use-
> less.

really?

>>> time.time()
973625208.45200002
>>> file = open("foo", "w")
>>> file.write("%d" % time.time())
>>> file.close()
>>> file = open("foo")
>>> file.read()
'973625212'
>>>

maybe you used readline()[:-1], and forgot that there
is no newline at the end of the file?

to solve that problem, use:

    file.write("%d\n" % time.time())

or (probably better):

    timestamp = string.strip(file.readline())

</F>





More information about the Python-list mailing list