file handling table file object attributes and methods # syntax description seekable(returns true if supports random access tell( truncatesizereturns the current file pointer position relative to the start of the file truncates the file to the current file pointer positionor to the given size if size is specified writable(returns true if was opened for writing write(swrites bytes/bytearray object to the file if opened in binary mode or str object to the file if opened in text mode writelinesseqwrites the sequence of objects (strings for text filesbyte strings for binary filesto the file we can treat the file like list using the item access operator ([])here we assign two byte strings (bytes objectseach containing an encoded string and an integerat two record index positions in the file these assignments will overwrite any existing contentand if the file doesn' already have six recordsthe earlier records will be created with every byte set to contact_data contact unpack(contacts[ ]contact_data[ decode("utf "rstrip(chr( )returns'cindy dovesince the string "cindy doveis shorter than the utf- characters in the structwhen it is packed it is padded with bytes at the end so when we retrieve the recordthe contact_data will hold the -tuple ( 'cindy dove\ \ \ \ \ ' to get the namewe must decode the utf- to produce unicode stringand strip off the padding bytes now that we've had glimpse of the class in actionwe are ready to review the code the binaryrecordfile binaryrecordfile class is in file binaryrecordfile py after the usual preliminaries the file begins with the definitions of couple of private byte values_deleted "\ _okay "\ each record starts with "statebyte which is either _deleted or _okay (or "\ in the case of blank recordshere is the class line and the initializerclass binaryrecordfiledef __init__(selffilenamerecord_sizeauto_flush=true)self __record_size record_size