Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop shutil.make_archive adding archive to itself

I have App dir inside Release dir

$ cd Release
$ tree
.
`-- App
    |-- App.exe
    ..........

and I am trying to create App-1.0.zip in the Release dir containg App with all its content. That is after unpacking App-1.0.zip I would get this App dir.

I tried shutil.make_archive but when I do this

import shutil

shutil.make_archive('App-1.0', 'zip', '.')

from Release dir, I get 48 byte App-1.0.zip inside App-1.0.zip besides the App dir. That is it adds this unfinished archive to itself.

Is there any way to avoid that except creating the archive in temp dir and moving?

I tried to set base_dir and use App as root_dir

shutil.make_archive('App-1.0', 'zip', 'App', 'App')

but I get error that App is not found when I set base_dir.

Traceback (most recent call last):
  File ".......archive.py", line 4, in <module>
    shutil.make_archive('App-1.0', 'zip', 'App', 'App')
  File "C:\Users\Alex\.virtualenvs\....-nAKWzegL\lib\shutil.py", line 800, in make_archive
    filename = func(base_name, base_dir, **kwargs)
  File "C:\Users\Alex\.virtualenvs\....-nAKWzegL\lib\shutil.py", line 686, in _make_zipfile
    zf.write(path, path)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\Lib\zipfile.py", line 1594, in write
    zinfo = ZipInfo.from_file(filename, arcname)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python36-32\Lib\zipfile.py", line 484, in from_file
    st = os.stat(filename)
FileNotFoundError: [WinError 2] The system cannot find the file specified: "'App'"

The same for '/App' and './App'. With full path it works, but I get all parent dirs, not just App.

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] on win32

like image 325
Alex P. Avatar asked Mar 24 '18 17:03

Alex P.


2 Answers

What I learned while fighting with this is this:

shutil.make_archive('App-1.0', 'zip', '.', 'App')

While 'App-1.0' is technically a "filename", it must be represented as a path to the file without the extension ('c:\myfiles\myzipfile'). I have not played with all the variants for path names yet, so some of the shortcuts likely work (such as: 'Release/App-1.0').

like image 21
USMCBacklash Avatar answered Sep 22 '22 06:09

USMCBacklash


Here's a couple of solutions that worked or me:

# curdir: Release
shutil.make_archive('App-1.0', 'zip', '.', 'App')

# curdir: ../Release
shutil.make_archive('Release/App-1.0', 'zip', 'Release', 'App')
like image 137
ekhumoro Avatar answered Sep 23 '22 06:09

ekhumoro