How to read and write binary file

Read and write binary file


F = open('data.bin', 'wb')
# from  w w  w .  j  a  va 2s .c  om
import struct 
bytes = struct.pack('abc', 7, 'spam', 8)
print bytes 
F.write(bytes)                            
F.close(  ) 

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




















Home »
  Python »
    Advanced Features »




Exception Handling
File
Module