python - 命令行 tar 和 Shutil.make_archive 之间的区别?

标签 python tar

当我使用 tar cfz cl.tar.gz Sources 生成 tar.gz 文件时,与 python shutil.make_archive ( "py", "gztar", "Sources"相比),我得到不同的结果:

$ tar -tf cl.tar.gz | head -3
algorithm/
aligned_storage.hpp
algorithm/cxx11/


$ tar -tf py.tar.gz | head -3
./algorithm/
./aligned_storage.hpp
./algorithm/cxx11/

大多数时候这没有什么区别;但如果您在解压缩时传递 --strip-components ,则两个存档的行为会有所不同。

我真的很想从 python 代码中获得与从 shell 脚本中获得的结构相同的结构,但我不太确定如何做到这一点。我在 Mac OS 10.8.1 上使用 Python 2.7.3,如果这有影响的话。

最佳答案

make_archive函数采用两个参数:root_dirbase_dir。如果您将 base_dir 设置为其他值,它将删除 ./ 前缀。例如:

$ tree
.
├── blah
│   ├── file1
│   └── file2

$ python
>>> import shutil
>>> shutil.make_archive("test", "gztar", ".", "blah")

$ tar -tf test.tar.gz 
blah/
blah/file1
blah/file2

这仅限于单个目录。如果您想添加多个这样的目录或文件,则必须使用 tarfile直接模块。

关于python - 命令行 tar 和 Shutil.make_archive 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12307880/

相关文章:

git - 压缩包含 `.git` 目录的 git 存储库目录

python - 12 个请求后(约 60 秒)Firebase 或 Python-firebasin 库不发送和接收数据

python - 如何在 Django 中创建自定义混合?

linux - 将文件解压到新文件夹中

unix - tar有助于压缩吗?

linux - 为什么 tar -c 命令从成员名称中删除前导 `/'

ubuntu - 将大量(100 万+)文件从一台服务器移动到另一台服务器 - 'too many arguments'

python - NDB 查询在结构化属性上使用过滤器,这也是重复的?

python - 我有 Numpy 32 位还是 64 位?

python - 使用正则表达式重命名 Pandas 数据框中的列