Struct.pack ():
Struct.pack is used to convert Python values from a format character to a string (because there is no byte type in Python, you can interpret the string here as a byte stream, or an array of bytes). Its function prototype is: Struct.pack (FMT, V1, v2, ...) , the parameter FMT is a format string, with information about the format string below some introduction. V1, v2, ... Represents the Python value to convert.
Struct.unpack
Struct.unpack does work just as opposed to struct.pack, which is used to convert byte streams into Python data types. The function prototype is: Struct.unpack (FMT, String), which returns a tuple.
1F= Open (file_name,'RB')2F.seek (0,2)3data_len=F.tell ()4 while1:5 ifF.tell () = =Data_len:6 Print 'file is end'7 Break8 Else:9data = F.read (2)Ten(ident,) = Struct.unpack (endianness +'H', Data[0:2]) OneF.close ()
Python binary file parsing