How can I read multiple lines in a file faster with python?

I am currently using the following python code:

file = open(filePath, "r")
lines=file.readlines()
file.close()

Say my file has several lines (10,000 or more), then my program becomes slow if I do this for several files. Is there a way to speed this up in Python? Reading various links I understand that readlines stores the lines of a file in memory, so the code slows down.

I also tried using the following code, and the time I got is 17%.

lines=[line for line in open(filePath,"r")]

Is there any other module in python2.4 (which I could skip). Thanks, Sandhya

+3
source share
1 answer
for line in file:

This gives you an iterator that reads the file object one line at a time, and then discards the previous line from memory.

- , , iter (f) f ( f ). , for (, f: print), next() . StopIteration EOF. for ( ), next() . , next() (, readline()) . () . 2.3.

Short answer: do not assign lines to a variable, just perform any operations that you need inside the loop.

+5
source

Source: https://habr.com/ru/post/1789922/


All Articles