How does a for loop over a file work in Python? -


i have piece of code

f = open('textfile.txt', 'r') line in f:     print line 

lets textfile.txt this

1 2 3 4 5 

how work? how know in file? understand printing on , on why doesn't print whole file on , over. don't see how f range. assume knows stop @ eof?

calling open() returns file object - i.e. f file object. file objects own iterators, implementing next() method, allowing them used in for loops per example. , yes, iterator implementation knows stop @ eof. have @ description here, under file.next() method details: http://docs.python.org/2/library/stdtypes.html#bltin-file-objects


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -