file open('random bin''rb'file seek(reclen file read(reclenb'yyyyyyyybinary mode works the same here fetch record returns byte strings but unless your file' content is always simple unencoded text form like ascii and has no translated line-endstext mode should not generally be used if you are going to seek--line-ends may be translated on windows and unicode encodings may make arbitrary transformationsboth of which can make absolute seek offsets difficult to use in the followingfor examplethe positions of characters after the first non-ascii no longer match between the string in python and its encoded representation on the filedata 'sp\xe mdatalen(data('spam' data encode('utf ')len(data encode('utf ')( 'sp\xc \xa ' data to your script unicode chars nonascii bytes written to file open('test'mode=' +'encoding='utf 'use text modeencoded write(dataf flush( seek( ) read( ascii bytes work 'sf seek( ) read( as does -byte nonascii 'adata[ but offset is not ' 'mf seek( ) read( unicodedecodeerror'utf codec can' decode byte xa in position unexpected code byte as you can seepython' file modes provide flexible file processing for programs that require it in factthe os module offers even more file processing optionsas the next section describes lower-level file tools in the os module the os module contains an additional set of file-processing functions that are distinct from the built-in file object tools demonstrated in previous examples for instancehere is partial list of os file-related callsos openpathflagsmode opens file and returns its descriptor os readdescriptorn reads at most bytes and returns byte string os writedescriptorstring writes bytes in byte string string to the file file tools