python 拷貝一個目錄
阿新 • • 發佈:2018-12-14
# 拷貝目錄 def copy_dir(src, dst): # 同一地址無需拷貝 if os.path.abspath(src) == os.path.abspath(dst): print('地址相同,無需拷貝') return # 判斷原始檔是否是普通檔案 if os.path.isfile(src): print(src, '是檔案,無法拷貝') return # 目標地址不存在 if not os.path.exists(dst): os.makedirs(dst) # 目標地址不是目錄 if not os.path.isdir(dst): print(dst, '目標地址不是目錄,無法拷貝') return # 目標地址是目錄,遞迴拷貝 dirs = os.listdir(src) for f in dirs: # 目錄拼接 src_file = os.path.join(src, f) dst_file = os.path.join(dst, f) # 分類處理 if os.path.isfile(src_file): # 是檔案,直接拷貝 copy(src_file, dst_file) else: # 是目錄,遞迴拷貝 copy_dir(src_file, dst_file) copy_dir('test', 'test2/ttt')