4

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

...

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.

scharette
  • 9,437
  • 8
  • 33
  • 67
  • @ekhumoro Isn't the linked duplicate newer than my question ? Why would this thread be closed and not the other one ? – scharette Oct 01 '19 at 02:15
  • Your question didn't have any answers when the other one was asked, so neither could be marked as a duplicate at the time. There isn't much to choose between the two questions now, except that the other one has had a little more activity (i.e. the comments). Both questions still have value, so the important thing was to link them together so that others can find them more easily, – ekhumoro Oct 01 '19 at 11:10

1 Answers1

-1

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.