python多線程實現文件夾拷貝


import threading
import os
import shutil


# 創建拷貝任務
def copy_work(source_dir, dest_dir, file_name):
# 拼接文件名路徑
source_file_path = source_dir + '/' + file_name
dest_file_path = dest_dir + '/' + file_name
# 打開目標文件
with open(dest_file_path, 'wb') as dest_file:
# 打開源文件
with open(source_file_path, 'rb') as source_file:
# 寫入數據
while True:
source_file_data = source_file.read(1024)
if source_file_data:
dest_file.write(source_file_data)
else:
break


if __name__ == '__main__':
# 指定源目錄和目標目錄
source_dir = input("輸入源目錄:")
dest_dir = input("輸入目標目錄")
if os.path.exists(source_dir):
if os.path.exists(dest_dir):
# shutil.rmtree(dest_dir)
print("目標文件夾已存在,如果目錄內存在同名文件,將覆蓋")
else:
# 創建目標文件夾
os.mkdir(dest_dir)
# 獲取源目錄文件列表
source_file_list = os.listdir(source_dir)
print(source_file_list)
for file_name in source_file_list:
copy_thread = threading.Thread(target=copy_work, args=(source_dir, dest_dir, file_name))
copy_thread.start()
else:
print("請確認源目錄是否存在或者是否拼寫錯誤")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM