Simple File I/O Question

Grant Edwards ge at nowhere.none
Thu Jul 13 12:57:36 EDT 2000


In article <396DF13A.97CC3F5E at bioeng.ucsd.edu>, Curtis Jensen wrote:
>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

the write method only accepts strings as parameters, so you've
got to convert whatever you want to write to a string before
passing it to write()

try this:

  f.write(str(var))
    or
  f.write("The value of 'var' is %d\n" % var)

-- 
Grant Edwards                   grante             Yow!  Yow! Are we wet yet?
                                  at               
                               visi.com            



More information about the Python-list mailing list