2014-11-02 84 views
1

使用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() 

回答

0

你可以解決它通過使用成員返回的TarInfo對象的islink()方法。類似如下:

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