I am trying to learn python and encounter the following snippet of code
f = open('test.txt')
for line in f:
print line
It prints the content in my file, line by line.
I understand for loop's basic concept: for x in list
when list
is a list it will go through all the element in the list. My question is, f
is a file object, not a list, how come the for loop magically understands there are lines in the file and process it one by one?
If someone can explain what happen exactly with the for loop in the above code that will be very helpful. Thanks!