-1

I'm trying to integrate tqdm progress bar in a loop.

Following what suggested here I wrote this:


from tqdm import tqdm
from time import sleep

totalFiles = 0
totalDir = 0

for base, dirs, files in os.walk(myFile):
        #print('Searching in : ',base)
        for directories in dirs:
            totalDir += 1
        for Files in files:
            totalFiles += 1

for root, dirs, files in os.walk(myFile):

            for item in tqdm((files), total=totalFiles):
                sleep(0.01)
        
             do something 

However, as result I'm having the following output

0/12849 [00:00<?, ?it/s] 0% 
0/12849 [00:00<?, ?it/s] 0%       
0/12849 [00:00<?, ?it/s] 0%
0/12849 [00:00<?, ?it/s] 0%
0/12849 [00:00<?, ?it/s] 0%

...

Suggestions how to solve this issue?

Thanks,

Pelide
  • 468
  • 1
  • 4
  • 19

1 Answers1

1

Your approach looks good, but you are using in tqdm argument "total=" which can produced some distraction when you iterate through an empty list

>>> from tqdm import tqdm
>>> from time import sleep
>>> for root, dirs, files in [[1,2,[1,2,3]],[1,2,[1,2,3]]]:
...             for item in tqdm((files)):
...                 sleep(0.01)
100%|██████████| 3/3 [00:00<00:00, 65.29it/s]
100%|██████████| 3/3 [00:00<00:00, 65.29it/s]
>>> for root, dirs, files in [[1,2,[]],[1,2,[]]]:
...             for item in tqdm(files, total=12849 ):
...                 sleep(0.01)

  0%|          | 0/12849 [00:00<?, ?it/s]
  0%|          | 0/12849 [00:00<?, ?it/s]

You don't show code where you get: root, dirs, files, so i can't know where exactly is the problem but i recommend you to check that part of the code.

userkk
  • 121
  • 4