Mega Code Archive

 
Categories / Python Tutorial / File
 

Storing and parsing packed binary data in files

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