Python pathlib readlines

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2023 Mar 26 2:41
Editor
Edited
Edited
2023 Oct 12 15:47
Refs
Refs
3 solutions
  • Use fileobject.readlines() or for 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.
 
 
 
 
 
 
 
 
 

Recommendations