to open binary filewe modify the mode string to append 'bso'wbwould open file for writing byteswhile 'rballows us to read them they will behave like text filesbut without the automatic encoding of text to bytes when we read such fileit will return bytes objects instead of strand when we write to itit will fail if we try to pass text object these mode strings for controlling how files are opened are rather cryptic and are neither pythonic nor object-oriented howeverthey are consistent with virtually every other programming language out there file / is one of the fundamental jobs an operating system has to handleand all programming languages have to talk to the os using the same system calls just be glad that python returns file object with useful methods instead of the integer that most major operating systems use to identify file handleonce file is opened for readingwe can call the readreadlineor readlines methods to get the contents of the file the read method returns the entire contents of the file as str or bytes objectdepending on whether there is 'bin the mode be careful not to use this method without arguments on huge files you don' want to find out what happens if you try to load that much data into memoryit is also possible to read fixed number of bytes from filewe pass an integer argument to the read method describing how many bytes we want to read the next call to read will load the next sequence of bytesand so on we can do this inside while loop to read the entire file in manageable chunks the readline method returns single line from the file (where each line ends in newlinea carriage returnor bothdepending on the operating system on which the file was createdwe can call it repeatedly to get additional lines the plural readlines method returns list of all the lines in the file like the read methodit' not safe to use on very large files these two methods even work when the file is open in bytes modebut it only makes sense if we are parsing text-like data that has newlines at reasonable positions an image or audio filefor examplewill not have newline characters in it (unless the newline byte happened to represent certain pixel or sound)so applying readline wouldn' make sense for readabilityand to avoid reading large file into memory at onceit is often better to use for loop directly on file object for text filesit will read each lineone at timeand we can process it inside the loop body for binary filesit' better to read fixed-sized chunks of data using the read(methodpassing parameter for the maximum number of bytes to read