pythonreference.com

search statements, functions, methods, exceptions*

show categories

'for' with a file
iterate over each string line in a file
fh = open('thisfile.txt')

for myline in fh:
    print(myline)
for str in file: print str
Argument(s):
N/A (iterates over file object itself)
Return value(s):
with each iteration, a string line from the file, assigned to loop variable
as Python iterates over each line in the file, it assigns the line to the loop variable (in the example, 'myline') as a string, then executes the block. The assignment and block execution happen once for each line in the file