Creating binary files

greg Landrum greglandrum at earthlink.net
Wed May 24 10:16:14 EDT 2000


Anders M Eriksson wrote:
> 
> I know how to open a binary file
> 
> bfile = open("bfile","wb")
> 
> but how do I write an integer into the file?
> 

There's probably a better way to do this, but I use the standard
'struct' module:
import struct
foo = open('foofile','wb')
bar = 666
foo.write(struct.pack('i',bar))

then, to read the file:
foo = open('foofile','rb') 
struct.unpack('i',foo.read(4)) 

Hope this helps
-greg

-- 

greg Landrum (greglandrum at earthlink.net)
Software Carpenter/Computational Chemist



More information about the Python-list mailing list