在创建由文件夹和文件组成的文件夹的 .tar 文件时收到以下警告:
“tar:从成员名称中删除前导‘/’”
Tarfile 是使用以下代码片段创建的。我当前目录中有 sample 文件夹,我用它在 tar 文件中添加文件但是,我想摆脱上述警告:
def create_tarball(self, dir_name):
try:
with tarfile.open("example.tar", "w:tar") as tar:
tar.add("./sample/"+dir_name, arcname=os.path.basename(".sample/"))
except Exception as e:
raise(e)
我已经看到了命令行的解决方案,例如使用-C。但是我如何在这段Python代码中实现它呢?
解决方案:将 / 添加到 os.path.basename 参数中的“./sample/”。更新后的代码片段:
def create_tarball(self, dir_name):
try:
with tarfile.open("example.tar", "w:tar") as tar:
tar.add("./sample/"+dir_name, arcname=os.path.basename("./sample/"))
except Exception as e:
raise(e)