python关于tar相对路径和绝对路径打包tar.gz文件示例

# -*- coding: utf-8 -*-
"""
Created on Tue Nov 27 10:52:57 2018
@author: issta
"""
import os 
import tarfile 
 
__author__ = 'Administrator'
RelativeOrAbsoluteTar = True #False
 
def archive(): 
    cwd = os.getcwd() 
    tar = tarfile.open('IsstaGao'+".tar.gz", 'w:gz') 
    for root, dirs, files in os.walk(cwd): 
        for file in files: 
            fullpath = os.path.join(root,file)
           
            if RelativeOrAbsoluteTar :
                used_path = os.path.relpath(fullpath, start=os.curdir)
                #relative_path = os.path.relpath(fullpath, start = cwd)
            else :
                used_path = fullpath
           
            tar.add(used_path)
    tar.close()
 
if __name__=='__main__': 
    archive()

------------------运行结果:

python关于tar相对路径和绝对路径打包tar.gz文件示例


猜你喜欢

转载自blog.csdn.net/lm393485/article/details/88894299