[英]Python - Copy folders from one directory to another directory (Copy only that folders that i have included in a txt files)
[英]python - How to copy files from one directory to another only if the file is not present in destination?
如果 dest_dir 中不存在該文件,我想按文件名檢查文件並從 src_dir 復制到各自文件夾中的 dest_dir。
例如,我有一個
src_dir
dest_dir
第一個文件夾
第二個文件夾
第三個文件夾
另一個文件夾
結果應該是,
dest_dir
第一個文件夾
第二個文件夾
第三個文件夾
另一個文件夾
這里部分回答。
import os
from shutil import copyfile
filename = 'file.ext'
src_dir = 'src/'
dst_dir = 'dst/'
if not os.path.isfile(dst_dir + filename):
copyfile(src_dir + filename, dst_dir + filename)
如果您希望對目錄中的每個文件都這樣做:
import os
from shutil import copyfile
import glob
src_dir = 'src/'
dst_dir = 'dst/'
for file_path in glob.glob(os.path.join(src_dir, '*')):
filename = os.path.basename(file_path)
if os.path.isfile(file_path) and not os.path.isfile(dst_dir + filename):
copyfile(file_path, dst_dir + filename)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.