项目:选择性拷贝

# 项目:选择性拷贝

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值