My question is related to file-input in Python, using open()
. I have a text file mytext.txt
with 3 lines.
I am trying to do two things with this fi
Is there some reason you could not use the following:
input_file = open('mytext.txt', 'r')
count_lines = 0
for line in input_file:
print line
count_lines += 1
print 'number of lines:', count_lines
The thing returned by open is a file object. File objects keep track of their own internal position as you loop over them, so in order to do what you tried first, you would have to rewind it to the beginning manually, it won't do it by itself.