[1] 使用
将下面代码存到copy_files.py
中,在python运行环境下运行python copy_files.py -h
,即出现如下界面:
[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