A
A
Alexey2021-06-07 15:35:12
Python
Alexey, 2021-06-07 15:35:12

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

1 answer(s)
T
Ternick, 2021-06-07
@skygliderus

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:

Code

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)

In general, on stackoverflow, there is a whole article that discusses different options for getting the number of lines in a file, you can choose a more optimized one if this one does not suit you.
Documentation regarding tqdm.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question