3 solutions
- Use
fileobject.readlines()
orfor line in fileobject
as a quick solution for small files.
- Use
linecache
for a more elegant solution, which will be quite fast for reading many files, possible repeatedly.
- Take @Alok's advice and use
enumerate()
for files which could be very large, and won't fit into memory. Note that using this method might slow because the file is read sequentially.