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
Post a Comment