
如何使用 Python 多线程复制文件并显示进度条
要使用 Python 多线程复制文件并显示进度条,您可以遵循以下步骤:
1. 导入必要的库
import os import shutil import threading from tqdm import tqdm
2. 定义复制文件函数
def copy_file(src, dst):
shutil.copyfile(src, dst)
3. 定义进度条函数
def progress_bar(total):
bar = tqdm(total=total, unit='B', unit_scale=True)
return bar
4. 创建多线程
def copy_files(src_dir, dst_dir):
threads = []
for file in os.listdir(src_dir):
src = os.path.join(src_dir, file)
dst = os.path.join(dst_dir, file)
bar = progress_bar(os.path.getsize(src))
thread = threading.Thread(target=copy_file, args=(
src, dst, bar))
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
5. 调用多线程函数
if __name__ == "__main__":
src_dir = "source_directory"
dst_dir = "destination_directory"
copy_files(src_dir, dst_dir)
示例用法
src_dir = "source_directory" dst_dir = "destination_directory" copy_files(src_dir, dst_dir)
运行结果
该代码将复制给定目录中的所有文件到另一个目录,并在复制时显示进度条。
以上就是python多线程复制文件进度条 python多线程复制文件显示进度条的详细内容,更多请关注知识资源分享宝库其它相关文章!







发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。