如何使用 tqdm 在 python 中添加进度条

how to add progress bar in python using tqdm

我有一个列出文件和目录并复制所需文件和目录的脚本。现在我需要添加一个 进度条 来显示使用 tqdm package

的复制进度

问题是我不知道在哪里进行 tqdm 的迭代以获得我想要的结果。

代码:

numfile = len(files)
for  file in files:
            full_file_name = os.path.join(dirpath, file)
            if os.path.join(dirpath) == src:
                if file.endswith("pdf"):
                    if not os.path.exists(dst2):
                        os.mkdir(dst2)
                    else:
                        print("the path alredy exist")
                     shutil.copy(full_file_name, dst2)
                    i+=1
                    
                elif file.endswith("docx") or file.endswith("doc"):
                     shutil.copy(full_file_name, dst)
                    j+=1

            elif os.path.join(dirpath)== src2:
                if file.endswith("pdf"):
                     shutil.copy(full_file_name, dst3)
                    z+=1
        
         for z in tqdm(range(numfile)):
            sleep(.1)

        print("*******number of directories = {}".format(len(dirnames)))
        print("*******number of files = {}".format(len(files)))   

现在它复制文件而不是显示进度条。

我要的是复制的时候显示进度条

使用tqdm跟踪外循环的进度。

for file in tqdm(files):
    # Copy file

在您当前的代码中,进度条仅显示 numfiles/10 秒的睡眠进度。这是毫无意义的,每次复制文件时都会导致您的代码休眠 numfiles/10s。