programsand so on to illustratepython' struct module can both create and unpack packed binary data--raw bytes that record values that are not python objects--to be written to file in binary mode we'll study this technique in detail later in the bookbut the concept is simplethe following creates binary file in python (binary files work the same in xbut the "bstring literal prefix isn' required and won' be displayed)import struct packed struct pack('> sh' 'spam' packed '\ \ \ \ spam\ \ file open('data bin''wb'file write(packed file close(create packed binary data bytesnot objects or text open binary output file write packed binary data reading binary data back is essentially symmetricnot all programs need to tread so deeply into the low-level realm of bytesbut binary files make this easy in pythondata open('data bin''rb'read(data '\ \ \ \ spam\ \ data[ : 'spamlist(data[ struct unpack('> sh'data( 'spam' open/read binary data file bytesunaltered slice bytes in the middle sequence of -bit bytes unpack into objects again unicode text files text files are used to process all sorts of text-based datafrom memos to email content to json and xml documents in today' broader interconnected worldthoughwe can' really talk about text without also asking "what kind?"--you must also know the text' unicode encoding type if either it differs from your platform' defaultor you can' rely on that default for data portability reasons luckilythis is easier than it may sound to access files containing non-ascii unicode text of the sort introduced earlier in this we simply pass in an encoding name if the text in the file doesn' match the default encoding for our platform in this modepython text files automatically encode on writes and decode on reads per the encoding scheme name you provide in python xs 'sp\xc ms 'spams[ 'afile open('unidata txt'' 'encoding='utf- ' introducing python object types non-ascii unicode text sequence of characters write/encode utf- text