Storing and parsing packed binary data in files : Binary File « File « Python Tutorial






F = open('data.bin', 'wb')

import struct 
bytes = struct.pack('>i4sh', 7, 'spam', 8)
print bytes 
F.write(bytes)                            
F.close(  ) 

F = open('data.bin', 'rb') 
data = F.read(  )                         
print data 
values = struct.unpack('>i4sh', data)     
print values








12.2.Binary File
12.2.1.Storing and parsing packed binary data in files