import os
import shutil
# 设置源文件夹和目标文件夹
source_folder = r'C:\Users\Administrator\Desktop\test\source' # 源文件夹
destination_folder = r'C:\Users\Administrator\Desktop\test\goal' # 目标文件夹
imgType = '.png' # 图片类型
# 确保目标文件夹存在
os.makedirs(destination_folder, exist_ok=True)
# 打开包含文件名的txt文件
with open('name.txt', 'r') as file:
filenames = file.readlines()
with open('isNotExit.txt', 'w') as f:
# 移动每个文件到目标文件夹
for filename in filenames:
filename = filename.strip() + imgType # 去除可能的换行符
source_path = os.path.join(source_folder, filename)
destination_path = os.path.join(destination_folder, filename)
# 检查文件是否存在,并且只有在文件存在时才移动它
if os.path.isfile(source_path):
# shutil.move(source_path, destination_path) # 移动
shutil.copy2(source_path, destination_path) # 复制
else:
f.write(filename + "\n")
print(f"The file {filename} does not exist and cannot be moved.")
f.close()