in older versions of pythonthe traditional way to read file line by line in for loop was to read the file into list that could be stepped through as usualfile open('data txt'for line in file readlines()print(lineend=''don' do this anymoreif you've already studied the core language using first book like learning pythonyou may already know that this coding pattern is actually more work than is needed today-both for you and your computer' memory in recent pythonsthe file object includes an iterator which is smart enough to grab just one line per request in all iteration contextsincluding for loops and list comprehensions the practical benefit of this extension is that you no longer need to call readlines in for loop to scan line by line--the iterator reads lines on request automaticallyfile open('data txt'for line in fileprint(lineend=''hello file worldbye file world no need to call readlines iterator reads next line each time better stillyou can open the file in the loop statement itselfas temporary which will be automatically closed on garbage collection when the loop ends (that' normally the file' sole reference)for line in open('data txt')print(lineend=''hello file worldbye file world even shortertemporary file object auto-closed when garbage collected moreoverthis file line-iterator form does not load the entire file into line' list all at onceso it will be more space efficient for large text files because of thatthis is the prescribed way to read line by line today if you want to see what really happens inside the for loopyou can use the iterator manuallyit' just __next__ method (run by the next built-in function)which is similar to calling the readline method each time throughexcept that read methods return an empty string at end-of-file (eofand the iterator raises an exception to end the iterationfile open('data txt'file readline('hello file world!\nfile readline('bye file world \nfile readline('read methodsempty at eof file open('data txt'file __next__('hello file world!\niteratorsexception at eof no need to call iter(filefirstsince files are their own iterator file tools