如何将 tqdm 进度条与 python 中的搜索文件绑定

How to bind tqdm progressbar with searching files in python

我编写了一个使用 tqdm 进度条搜索文件的函数。但是如何在列出文件时编写代码来避免这种影响(图片): 我只想要同时加载列表文件的单个进度条。

        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))

迭代时使用 tqdm:

        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))