3 solutions
- Use
fileobject.readlines()orfor line in fileobjectas a quick solution for small files.
- Use
linecachefor 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.

Seonglae Cho