Simple File I/O Question

Mike Fletcher mfletch at tpresence.com
Thu Jul 13 13:06:31 EDT 2000


str( var ) # gives '1'

or

import struct
struct.pack( '>i', var ) # binary representation, gives '\000\000\000\001'

or

import pickle # or cPickle
pickle.dumps( var ) # allows for arbitrary objects to be stored/restored,
gives 'I1\012.'

or

pickle.dumps( var, 1) # uses more compact binary representation, otherwise
identical, gives 'K\001.'

or

look at the shelve module (name:object storage)

See the docs on struct, shelve and pickle for explanations of their purpose
and operation.  HTH,
Mike

-----Original Message-----
From: Curtis Jensen [mailto:cjensen at bioeng.ucsd.edu]
Sent: Thursday, July 13, 2000 12:42 PM
To: python-list at python.org
Subject: Simple File I/O Question


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
-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list