Simple File I/O Question

richard_chamberlain richard_chamberlain at ntlworld.com
Thu Jul 13 13:05:12 EDT 2000


Hi Curtis,

Try converting you int to a string:

var=5
f=open('foo','w')
f.write(`var`)

or

f.write(str(var))

if it's clearer for you.

The problem with doing this is that to read the int back you'd need to
convert it back into an int, so it's a bit of pain if you have to do it
alot. If you want to store lots of values try using pickle.

Richard


Curtis Jensen <cjensen at bioeng.ucsd.edu> wrote in message
news:396DF13A.97CC3F5E at bioeng.ucsd.edu...
> I know this should be simple, but....  I have a variable that holds an
> int.  I want to write it to a file.  The following code:
>
> var = 5
> f = open('foo','w')
> f.write(var)
>
> gives this error:
> TypeError: read-only buffer, int
>
> This works if:
> var = '5'
>
> So, How do I write an int variable to a file?  Thanks.
>
> --
> Curtis Jensen
> cjensen at bioeng.ucsd.edu
> http://www-bioeng.ucsd.edu/~cjensen/
> FAX (425) 740-1451





More information about the Python-list mailing list