Shutil make_archive 导致嵌套的 .zip 文件 [英] shutil make_archive resulting in nested .zip files

查看:33
本文介绍了Shutil make_archive 导致嵌套的 .zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 make_archive 来自 shutil 库.

I've been trying to use make_archive from shutil library.

这是代码(带有全面的注释):

Here is the code (with comprehensive comments):

from shutil import make_archive
make_archive(
  'zipfile_name', 
  'zip',           # archive format
  root_dir=None,   # current working dir if None
  base_dir=None)   # cwd if None 

这是一个目录树的例子:

Here is an example of directory tree:

folder
│   script.py
└───subfolder1
│   │   file011.txt
│   │   file012.txt
│   │
└───subfolder2
│   │   file011.txt
│   │   file012.txt
│   │
...

如果我运行我的脚本(使用上面的代码),结果是:

If I run my script (with code above), the result is:

folder
│   script.py
└───subfolder1
│   │   file011.txt
│   │   file012.txt
│   │
└───subfolder2
│   │   file011.txt
│   │   file012.txt
│   │
└───zipfile_name.zip
│   │   same content as above (like expected)
│   │   zipfile_name.zip

...

为什么要创建一个额外的 zipfile_name.zip?另请注意,第二个存档无法打开,因为它无效.我知道之前是否没有人遇到过这个问题,但也许我的参数有误,这就是我问的原因.

Why is an additional zipfile_name.zip is created? Also, note that the second archive can't be open since it is invalid. I understand if no one has ever faced this issue before, but maybe my parameters are wrong and this is why I'm asking.

推荐答案

我也遇到了这个问题.这是解决问题的一个简单技巧:使用相对路径作为 base_name:

I also encountered this problem. Here's a simple trick to solve the issue: use a relative path as the base_name:

from shutil import make_archive
make_archive(
  '../zipfile_name', 
  'zip',           # archive format
  root_dir=None,   # current working dir if None
  base_dir=None)   # cwd if None 

存档将在父目录中创建,不会干扰正在存档的目录.

The archive will be created in the parent directory, not interfering with the directory that is being archived.

这篇关于Shutil make_archive 导致嵌套的 .zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆