Python+targzip如何使用符号压缩?

2024-04-26 13:08:49 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python3.4中,如何使用tar+gzip压缩和“follow symbolic link”特性?问题是:

  • 在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)
...

Tags: 文件ifmode模式taropenfiltermethod
1条回答
网友
1楼 · 发布于 2024-04-26 13:08:49

^{}^{}类方法的快捷方式,该方法反过来调用^{}构造函数。文档有点含糊,但从代码中可以明显看出,前两个将把dereference关键字参数和所有其他未使用的kwarg传递给TarFile构造函数。在

因此,您可以将dereference与它们中的任何一个一起使用,如果您只是将其作为关键字参数传递:

archive = tarfile.open(name='foo.tar.gz', mode='w:gz', dereference=True)

相关问题 更多 >