小编Bal*_*int的帖子

Python tarfile:如何使用 tar+gzip 压缩并跟随符号链接?

如何在 Python 3.4 中使用 tar+gzip 压缩和“跟随符号链接”功能?问题是:

  • tarfile.open() 支持“w:gz”模式,但不支持“取消引用”选项
  • tarfile.tarfile() 支持“取消引用”但不支持“w:gz”模式

代码:

...
mode = ""
if bckentry['method'] == "tar":
    mode = "w"
elif bckentry['method'] == "targz":
    mode = "w:gz"

archive = tarfile.TarFile(name=filepath, mode=mode)
archive.dereference = True if bckentry['followsym'] == "yes" else False
# archive = tarfile.open(filepath, mode=mode)

if bckentry['withpath'] == 'yes':
    for entry in bckentry['include_dirs']:
        archive.add(entry, filter=self.filter_tar)
elif bckentry['withpath'] == 'no':
    for entry in bckentry['include_dirs']:
        archive.add(entry, arcname=os.path.basename(entry), filter=self.filter_tar)
...
Run Code Online (Sandbox Code Playgroud)

python gzip tar dereference tarfile

5
推荐指数
1
解决办法
2641
查看次数

标签 统计

dereference ×1

gzip ×1

python ×1

tar ×1

tarfile ×1