Simple File I/O Question

Charlie Sorsby crs at quail.swcp.com
Thu Jul 13 20:14:31 EDT 2000


In article <4ymb5.894$6E.112638 at ptah.visi.com>,
Grant Edwards <ge at nowhere.none> wrote:
= 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)

If I may add my own question to that of the original poster:

What if one needs to send an int to a device that will use it
and needs it to be an int not a string?  E.g. suppose I need to
send a hex int via the serial port (/dev/cuaa0) to some device?  Is
this not possible in python?



-- 
NB:  While I welcome *reasonable* and *relevant* e-mail responses, any
e-mail with a *long* "To: list" or any mailing list may be treated as
spam and reported to the sender's ISP.
-- 
Best regards,

Charlie "Older than dirt" Sorsby     Edgewood,  NM     "I'm the NRA!"
      crs at swcp.com www.swcp.com/~crs      USA      Life Member since 1965



More information about the Python-list mailing list