In Python 2.6 and 2.7 I would have thought that these two constructs would be identical:
Method A
i=0
f=open('fred.txt','r')
for line in f.readline():
i+=1
print i
Method B
i=0
f=open('fred.txt','r')
for line in f:
i+=1
print i
However, when fred.txt grew to be 74,000 lines, with each line 2,684 characters in length, Method A prints 2685 while Method B prints 74000. Obviously, Method B is preferred, but why does Method A work for small files but fail for large files?
There’s a typo, it should be f.readlines()
. You’re reading one line and looping through each character in the line.
Both methods (readlines
vs iterating over the file directly) ought to give the same results, but readlines
will store the entire contents in memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With