Writing an integer to a file?!?

David Bolen db3l at fitlinxx.com
Wed Nov 8 01:50:52 CET 2000


Gabriel Ambuehl <gabriel_ambuehl-py at buz.ch> writes:

> Hello Fredrik,
> 
> Tuesday, November 07, 2000, 7:15:39 PM, you wrote:
> > the error message means "expected a character buffer,
> > got an integer".  to put it another way, "write" needs a
> > string, not an integer.
> > try:
> 
> >     f.write(str(int(time.time())))
> > or perhaps:
> 
> >     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 useless.
> f.write(str(int(time.time())) + " ")

Hmm, there's no reason that the f.read() won't return all of the
contents of the file, although if you gave it a small enough buffer,
you may have to call it more than once to get the entire value.

You aren't perhaps assuming that f.read() returns "lines" and
terminates each of them and your code is automatically truncating what
it thinks is a newline at the end?  If so, then that could kill the
last digit, since unless you explicitly wrote a newline to the file,
it didn't contain one.

If thats not it, then perhaps seeing a snippet of the code in question
might help identify the issue.  You shouldn't need to add the space,
unless you want it for some other reason than this one.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list