【问题标题】:python untar linux equivalent [closed]python untar linux等效[关闭]
【发布时间】:2020-03-16 18:58:41
【问题描述】:

执行以下操作的等效 linux 命令是什么:

>>> import tarfile
>>> filepath = "./backup.tar.gz"
>>> tar = tarfile.open(filepath, "r:gz")
>>> tar.extractall()
>>> tar.close()

我相信答案是这样的:

$ tar -xvzf ./backup.tar.gz .

但想确保这两件事完全一样。

【问题讨论】:

  • 您是否有与tar -xzf ./backup.tar.gz 相关的特定问题导致被问到问题? (Stack Overflow 问题应该基于特定问题的通常指导在这里和任何地方一样都适用)。
  • (...另外,像往常一样,关于 UNIX 工具命令行使用的问题通常更适合我们的姊妹站点 Unix & Linux)。
  • @felipsmartins 哦,完美。谢谢。

标签: python linux unix tar


【解决方案1】:

您可以使用tar 来执行此操作。

tar -xvf ./backup.tar.gz

-x = extract
-f = file
-v = verbose

编辑:

在某些实现中,您可能需要添加-z 标志。 这是来自tarman 页面的注释。

-z, --gunzip, --gzip
  (c mode only) Compress the resulting archive with gzip(1).  In extract or list modes, this option is ignored.  Note that, unlike other tar implementations, this implementation recognizes gzip compression automatically when reading archives.

【讨论】:

  • -v? OP 的 Python 代码不打印详细输出。
  • 只是我习惯使用 tar 总是包含 -v
  • 另外,您确实需要-z 来支持.gz 文件作为输入。
  • 这似乎取决于实现。 -z, --gunzip, --gzip (c mode only) Compress the resulting archive with gzip(1). In extract or list modes, this option is ignored. Note that, unlike other tar imple- mentations, this implementation recognizes gzip compression automatically when reading archives.
  • 点头。关于tar 没有成为 POSIX 标准的棘手问题之一;要获得保证跨平台兼容的答案,需要改用(POSIX 定义,尽管部署不那么广泛)pax 工具。
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2015-12-21
  • 1970-01-01
  • 2022-03-03
  • 2017-09-20
  • 2017-02-11
  • 1970-01-01
相关资源
最近更新 更多