码迷,mamicode.com
首页 > 编程语言 > 详细

python 打包成tar包

时间:2018-12-31 19:11:08      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:root   打包   .gz   join   out   source   roo   make   walk   

 

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))
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()
make_targz("test.tar.gz","a.txt") 

#or

make_targz_one_by_one("test.tar.gz","a.txt")

 

python 打包成tar包

标签:root   打包   .gz   join   out   source   roo   make   walk   

原文地址:https://www.cnblogs.com/sea-stream/p/10202537.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!