如何禁用创建硬链接

时间:2014-11-02 13:21:09

标签: python tar tarfile

使用python的tarfile模块,有没有办法指定tar的Q选项的等价物(注意:Haiku具体)?

tar --help
  -Q, --replace-hardlinks    replace hardlinks with corresponding symlink when
                             extracting

目前代码基本上是:

tarFile = tarfile.open(archiveFile, 'r')
members = None
if subdir:
    members = [
        member for member in tarFile.getmembers()
        if member.name.startswith(subdir)
    ]
tarFile.extractall(targetBaseDir, members)
tarFile.close()

1 个答案:

答案 0 :(得分:0)

您可以使用成员返回的islink()对象的TarInfo方法解决此问题。如下所示:

tarFile = tarfile.open(archiveFile, 'r')
   for member in tarFile.getmembers():
        if member.islnk():
             # code for handling links
        else:
            tarFile.extract(targetBaseDir, member)
tarFile.close()