while truechar file read( if not charbreak print(charread by character empty string means end-of-file for char in open('test txt'read()print(charthe for loop here also processes each characterbut it loads the file into memory all at once (and assumes it fits!to read by lines or blocks insteadyou can use while loop code like thisfile open('test txt'while trueline file readline(if not linebreak print(line rstrip()read line by line file open('test txt''rb'while truechunk file read( if not chunkbreak print(chunkread byte chunksup to bytes line already has \ you typically read binary data in blocks to read text files line by linethoughthe for loop tends to be easiest to code and the quickest to runfor line in open('test txt'readlines()print(line rstrip()for line in open('test txt')print(line rstrip()use iteratorsbest for text input both of these versions work in both python and the first uses the file read lines method to load file all at once into line-string listand the last example here relies on file iterators to automatically read one line on each loop iteration the last example is also generally the best option for text files--besides its simplicityit works for arbitrarily large files because it doesn' load the entire file into memory all at once the iterator version may also be the quickestthough / performance may vary per python line and release file readlines calls can still be usefulthough--to reverse file' linesfor exampleassuming its content can fit in memory the reversed built-in accepts sequencebut not an arbitrary iterable that generates valuesin other wordsa list worksbut file object doesn'tfor line in reversed(open('test txt'readlines())in some python codeyou may also see the name open replaced with file and the file object' older xreadlines method used to achieve the same effect as the file' automatic line iterator (it' like readlines but doesn' load the file into memory all at onceboth file and xreadlines are removed in python xbecause they are redundant you should generally avoid them in new code too--use file iterators and open call in recent releases--but they may pop up in older code and resources for loops