iMath
iMath

Reputation: 2478

directory hierarchy issue when using shutil.make_archive

I want to create a zip archive of the pip package , code as following

import shutil
import os
import pip
shutil.make_archive(os.path.join(os.getcwd(), 'pipzip'), 'zip', root_dir=pip.__path__[0])

but when

shutil.unpack_archive(os.path.join(os.getcwd(), 'pipzip.zip'))

I got a list of files and folders in current directory, which originally resides in the pip package , i.e.

['_internal', '_vendor', '__init__.py', '__main__.py', '__pycache__']

While I hope there is a directory named pip to contain the list of files and folders when shutil.unpack_archive, so what adjustment to shutil.make_archive should I do ?

BTW, I cannot grasp the use of shutil.make_archive even consulting the doc, I think the doc should update so that give a clear description .

Upvotes: 0

Views: 631

Answers (1)

Arghya Saha
Arghya Saha

Reputation: 5713

You were half way there. Basically you were specifying the root_dir and not the base_dir. You can do it by using the following snippet.

import shutil
import os
import pip
from pathlib import Path
shutil.make_archive(base_name=os.path.join(os.getcwd(), 'pipzip'), format='zip', root_dir=Path(pip.__path__[0]).parent, base_dir=Path(pip.__path__[0]).name)
shutil.unpack_archive(os.path.join(os.getcwd(), 'pipzip.zip'))

Upvotes: 1

Related Questions