day02

问题--复制文件夹内特定类型文件

如果要具备一定的通用性,设计成命令行执行。输入的参数包括,源文件夹,目的文件夹,文件后缀
python aaa.py c:1 c:2 txt
表示将c:1目录下的所有txt文件复制到c:2下

代码展示

import os
import shutil
import sys


def copy(src_path, dst_path, sty):
    # 获取源文件夹下的文件
    dirlist = os.listdir(src_path)
    for d in dirlist:
        # 将文件名分成 文件名和文件后缀
        name = d.split(".", 1)
        if sty in name[1]:
            old = src_path + '\' + name[0] + '.' + name[1]
            new = dst_path + '\' + name[0] + '.' + name[1]
            # print(old)
            shutil.copyfile(old, new)
    print('复制完成')


# copy(r'c:11', r'c:12', 'txt')
if __name__ == '__main__':
    list = sys.argv
    copy(list[1], list[2], list[3])
原文地址:https://www.cnblogs.com/qinling73/p/13579594.html