records [ 'ssssssss' 'pppppppp' 'aaaaaaaa' 'mmmmmmmm'file open('random bin'' + 'for rec in recordssize file write(recfile flush(pos file seek( print(file read() 'ssssssssppppppppaaaaaaaammmmmmmmwrite four records bytes for binary mode read entire file nowlet' reopen our file in + modethis mode allows both reads and writes againbut does not initialize the file to be empty this timewe seek and read in multiples of the size of data items ("records"storedto both fetch and update them at randomc:\temppython file open('random bin'' + 'print(file read() 'ssssssssppppppppaaaaaaaammmmmmmmrecord ' file seek( file write(recordfile seek(len(record file write( ' file seek( file read(len(record) 'ppppppppfile read(len(record) 'yyyyyyyyread entire file update first record update third record fetch second record fetch next (thirdrecord file seek( file read( 'xxxxxxxxppppppppyyyyyyyymmmmmmmmread entire file :\temptype random bin xxxxxxxxppppppppyyyyyyyymmmmmmmm the view outside python finallykeep in mind that seek can be used to achieve random accesseven if it' just for input the following seeks in multiples of record size to read (but not writefixedlength records at random notice that it also uses text modesince this data is simple ascii text bytes and has no line-endstext and binary modes work the same on this platformc:\temppython file open('random bin'' 'reclen file seek(reclen file read(reclen'mmmmmmmmfile seek(reclen file read(reclen file and directory tools text mode ok if no encoding/endlines fetch record fetch record