python tar 打包

 

import os
import tarfile

def make_targz_one_by_one(output_filename, source_dir):
    tar = tarfile.open(output_filename,"w:gz")
    for root,dir,files in os.walk(source_dir):
        for file in files:
            pathfile = os.path.join(root, file)
            tar.add(pathfile)
    tar.close()

 

import os
import tarfile


def make_targz(output_filename, source_dir):
    with tarfile.open(output_filename, "w:gz") as tar:
        tar.add(source_dir, arcname=os.path.basename(source_dir))

make_targz("/opt/bp/maps/map_0116.tar.gz","/opt/bp/maps/map_0116")

 

posted @ 2019-04-10 17:32  anobscureretreat  阅读(490)  评论(0编辑  收藏  举报