Bal*_*int 5 python gzip tar dereference tarfile
如何在 Python 3.4 中使用 tar+gzip 压缩和“跟随符号链接”功能?问题是:
代码:
...
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)
tarfile.open是类方法的快捷方式TarFile.open,它又调用TarFile构造函数。文档有点模糊,但从代码中可以明显看出,前两个会将dereference关键字参数和所有其他未使用的 kwargs 传递给TarFile构造函数。
dereference因此,如果您只是将其作为关键字参数传递,则可以与其中任何一个一起使用:
archive = tarfile.open(name='foo.tar.gz', mode='w:gz', dereference=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2641 次 |
| 最近记录: |