import os
import shutil
import copy
import pathlib
source_path = os.path.abspath(r’G:\ruijin\AD128~’)
source_path = os.path.abspath(r’C:\Users\86191\Desktop\beifen’)
target_path = os.path.abspath(r’E:\ruijinbold’)
filelist = os.listdir(source_path);
for files in filelist:
final_source_path = os.path.join(source_path, files) # 源文件
final_target_path = os.path.join(target_path, files) # 目标文件
if os.path.exists(final_target_path):
# 如果目标路径存在原文件夹的话就先删除
shutil.rmtree(final_target_path)
if not os.path.exists(final_target_path):
# 如果目标路径不存在原文件夹的话就创建
os.makedirs(final_target_path)
key1 = 'BOLD'
key2 = 'MPRAGE_AV'
for file in os.listdir(final_source_path):
# is file
# if os.path.isfile(file1 + '/' + file):
if key1 in file:
print('找到包含"' + key1 + '"字符的文件,绝对路径为----->' + final_source_path + '\\' + file)
print('复制到----->' + final_target_path + '\\' + file)
shutil.copytree(final_source_path + '\\' + file, final_target_path + '\\' + file)
if key2 in file:
print('找到包含"' + key2 + '"字符的文件,绝对路径为----->' + final_source_path + '\\' + file)
print('复制到----->' + final_target_path + '\\' + file)
shutil.copytree(final_source_path + '\\' + file, final_target_path + '\\' + file)
print('copy dir finished!')