python打包压缩指定目录下的指定类型文件

  • Post author:
  • Post category:python


import os
import datetime
import tarfile
import fnmatch

def find_spe_file(root, patterns=['*'], non_cludedir=[]):
    for root, dirnames, filenames in os.walk(root):
        for pattern in patterns:
            for filename in filenames:
                if fnmatch.fnmatch(filename, pattern):
                    #print(filename)
                    yield os.path.join(root, filename)

def cre_tarfile():
    args = ["*.jpg", "*.jepg"]
    now = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
    filename = "all_img_{0}.tar.gz".format(now)
    with tarfile.open(filename, mode='w:gz') as f:
        for item in find_spe_file(".", args):
            #print(item)
            f.add(item)

if __name__ == "__main__":
    cre_tarfile()



版权声明:本文为csdn2798694115原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。