[' ''bbb''cc''dddd'[' '' '' '' 'storing packed binary datastruct one other file-related note before we move onsome advanced applications also need to deal with packed binary datacreated perhaps by language program or network connection python' standard library includes tool to help in this domain--the struct module knows how to both compose and parse packed binary data in sensethis is another data-conversion tool that interprets strings in files as binary data we saw an overview of this tool in but let' take another quick look here for more perspective to create packed binary data fileopen it in 'wb(write binarymodeand pass struct format string and some python objects the format string used here means pack as -byte integera -character string (which must be bytes string as of python )and -byte integerall in big-endian form (other format codes handle padding bytesfloating-point numbersand more) open('data bin''wb'import struct data struct pack('> sh' 'spam' data '\ \ \ \ spam\ \ write(dataf close(open binary output file make packed binary data write byte string python creates binary bytes data stringwhich we write out to the file normally--this one consists mostly of nonprintable characters printed in hexadecimal escapesand is the same binary file we met earlier to parse the values out to normal python objectswe simply read the string back and unpack it using the same format string python extracts the values into normal python objects--integers and stringf open('data bin''rb'data read(data '\ \ \ \ spam\ \ values struct unpack('> sh'datavalues ( 'spam' get packed binary data convert to python objects binary data files are advanced and somewhat low-level tools that we won' cover in more detail herefor more helpsee the struct coverage in consult the python library manualor import struct and pass it to the help function interactively also note that you can use the binary file-processing modes 'wband 'rbto process simpler binary filesuch as an image or audio fileas whole without having to unpack its contentsin such cases your code might pass it unparsed to other files or tools files