Search code examples
pythontqdm

How to bind tqdm progressbar with searching files in python


I wrote a function which search files with tqdm progressbar. But how to write code to avoid this effect while listing files (image): enter image description here I want just single progressbar that loads at the same time listing files.

        counter = 0
            filepath = "." 
            ext = ".txt" # for example
            for fil in os.listdir(filepath):
                if fil.endswith(ext):
                    print(fil)
                    counter+=1
                    sleep(0.01)
                for i in tqdm(range(counter)):
                    i+=1
            print("\nNumber of found elements: "+str(counter))

Solution

  • Use tqdm while iterating:

            filepath = "." 
            ext = ".txt" # for example
            for fil in tqdm(os.listdir(filepath)):
                if fil.endswith(ext):
                    print(fil)
                    counter+=1
                    sleep(0.01)
            print("\nNumber of found elements: "+str(counter))