【问题标题】:how to add progress bar in python using tqdm如何使用 tqdm 在 python 中添加进度条
【发布时间】:2020-06-24 20:44:05
【问题描述】:

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

的复制进度

问题是我不知道在哪里对 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)))   

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

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

【问题讨论】:

标签: python copy progress-bar


【解决方案1】:

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

for file in tqdm(files):
    # Copy file

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

【讨论】: