【发布时间】:2023-09-02 22:24:01
【问题描述】:
我有一个 python 脚本,可以复制选定位置中存在的文件夹和文件。 此任务完美运行,现在我想在复制时显示进度条。
我使用 tqdm 包在控制台中显示进度条,效果很好,但问题是它在每个文件上显示进度条并每次遍历所有现有文件.
示例:如果一个文件夹包含 128 个文件,它将像这样显示进度条 128 次
100%---------128/128 100%---------128/128
我想要的是为所有正在复制的文件显示进度条 1 次。
代码:
i=0
j=0
z=0
for dirpath, dirnames, files in os.walk(src):
print(f'Found directory: {dirpath}')
if len(dirnames)==0 and len(files)==0:
print("this directory is empty")
pass
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"):
numfile = len(files)
# i think the for loop must not be in this part.
for z in enumerate(tqdm(numfile)):
sleep(.1)
shutil.copy(full_file_name, dst3)
z+=1
【问题讨论】: