# 项目:选择性拷贝 import os,shutil repeatFiles = [] copyFiles = [] # 定义 check_dirs 函数,判断开始目录和目标目录1,是否存在?2,是否是同一个目录? def check_dirs(startPath,targetPath): if not os.path.exists(startPath): print('工作目录不存在,请确认!') elif startPath == targetPath: userCheck = input('工作目录和目标目录相同,确认请输入 YES: ') if userCheck == 'YES': return False else: if not os.path.exists(targetPath): os.mkdir(targetPath) return True # 定义 check_file_extension_name 函数,检查文件拓展名 def check_file_extension_name(file,extension_name): ext = [ext for ext in extension_name] if file.split('.')[-1] in ext: return True else: return False # 定义 check_file_in_target_path 函数,检查文件是否在目标目录 def check_file_in_target_path(file,targetPath): if os.path.exists(os.path.join(targetPath, file)): return True else: return False # 定义 copy函数,将原目录按照拓展名拷贝到目标目录 def copy_files(startPath, targetPath, extension_name): if check_dirs(startPath,targetPath): for root,subdirs,files in os.walk(startPath): # 遍历目录树 if root == targetPath: print('目标目录也在扫描范围内,已自动忽略') continue else: for file in files: if check_file_extension_name(file, extension_name): if check_file_in_target_path(file, targetPath): repeatFiles.append(file) elif not check_file_in_target_path(file, targetPath): copyFiles.append(file) return repeatFiles,copyFiles startPath = 'E:\\Pycharm\\code\\alien_invasion' targetPath = 'E:\\Pycharm\\Python极客项目编程源代码\\copy' extension_name ='py' if __name__ == '__main__': copy_files(startPath, targetPath, extension_name) print('已完成复制的文件: \n', copyFiles) print('目标目录下重名的文件: \n', repeatFiles)
项目:选择性拷贝
最新推荐文章于 2024-07-17 21:04:45 发布