移动或复制指定文件类型的文件

[1] 使用
将下面代码存到copy_files.py中,在python运行环境下运行python copy_files.py -h,即出现如下界面:
copy_files.py的参数

[2] 参数说明

-h 查看帮助
-op 复制(移动)文件的所在目录
-np 复制(移动)文件的目标目录
-t 复制(移动)文件的文件类型
-c 选择是复制文件或移动文件,默认为复制

import argparse
import os,sys,re
import shutil

def move_files(root_path,des_path,cp_mv_type,file_type):
    cnt = 0
    for filepath,dirnames,filenames in os.walk(str(root_path)):
        for filename in filenames:
            if filename[-3:] == file_type:
                cnt += 1
                full_path = os.path.join(filepath,filename) # get abstract path of file
                if cp_mv_type == "cp":
                    flag = "copied."
                    shutil.copy(full_path, des_path)  # copy files  from full_path to des_path
                elif cp_mv_type == "mv":
                    flag = "moved."
                    shutil.move(full_path, des_path)  # move files  from full_path to des_path

    print(cnt,file_type,"files have been",flag)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="move specified type files")
    parser.add_argument("-op", "--oldpath", required=True, type=str, help="path from which files are moved")
    parser.add_argument("-np", "--newpath", required=True, type=str, help="path in which files are moved")
    parser.add_argument("-t", "--type", required=True, type=str, help="file type")
    parser.add_argument("-c", "--cp_or_mv", required=False, type=str, default="cp", help="choose copy or move files——move:mv copy:cp")
    Args = parser.parse_args()
    
    input_path = Args.oldpath
    out_path = Args.newpath
    cp_mv_type = Args.cp_or_mv
    file_type = Args.type
    # check output path is existed or not and make it if not
    if os.path.isdir(out_path) == True:
        pass
    else:
        os.mkdir(out_path)
    move_files(input_path,out_path,cp_mv_type,file_type)

[3]示例演示
prokka_result目录下的gff为后缀的文件复制到gffs目录下:

python copy_files.py -op ./prokka_result -np ./gffs -t gff -c cp
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值