Use File Iterator¶
Sourcery refactoring id: use-file-iterator
¶
Description¶
Use the in-built file iterator rather than calling readlines()
Before¶
with open("foo") as f:
for line in f.readlines():
print(line)
After¶
with open("foo") as f:
for line in f:
print(line)
Explanation¶
The file object that Python returns when you open a file is a lazy iterator over
that file's lines. This means that there is no need to call readlines
to
iterate over it. Iterating directly is shorter, and also does not load the whole
file into memory with a list object ( which readlines
does) so can be more
performant.