簡體   English   中英

如何使用 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 /10s 睡眠中的進度。 這是沒有意義的,並且每次復制文件時都會導致代碼休眠numfiles /10s。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM