Answer the question
In order to leave comments, you need to log in
How to implement a progressbar when processing a file in Python?
The question arose of processing a file with a large number of lines (more than a million). On the advice from the article, I decided to use tqdm .
But I can't figure out how to integrate it into the code. Due to the large file size, I do not want to create a list so as not to eat memory.
with open("test.txt") as file:
for line in file:
a = str.strip(line)
h = func_1(a)
func_2(a, h)
Answer the question
In order to leave comments, you need to log in
In general, the idea is really so-so, but you know better)
In any case, you need to know the number of lines in the file, therefore, the simplest option that I know is this:
from tqdm import tqdm
with open("test.txt") as file:
num_lines = sum(1 for line in file)
file.seek(0)
with tqdm(total = num_lines) as pbar:
for line in file:
a = str.strip(line)
h = func_1(a)
func_2(a, h)
pbar.update(1)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question