shutil.make_archive not zipping to correct destination
Asked Answered
P

2

7

As per the code below I am having issues with the zipping a directory using the python 3 shutil.make_archive function. The .testdir will be zipped but it is being zipped in /home/pi, instead of /home/pi/Backups.

zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(zip_loc, 'zip', zip_dest)

Could anyone explain what I am doing wrong?

Pinette answered 14/10, 2017 at 6:10 Comment(0)
A
14

Reading the docs here I came up with:

zip_loc = '/home/pi/.testdir'
zip_dest = '/home/pi/Backups/'
shutil.make_archive(base_dir=zip_loc, root_dir=zip_loc, format='zip', base_name=zip_dest)

From the docs:

base_name is the name of the file to create, including the path, minus any format-specific extension.

 

root_dir is a directory that will be the root directory of the archive; for example, we typically chdir into root_dir before creating the archive.

 

base_dir is the directory where we start archiving from; i.e. base_dir will be the common prefix of all files and directories in the archive.

 

root_dir and base_dir both default to the current directory.

Adinaadine answered 14/10, 2017 at 6:21 Comment(1)
base_name should add dest pathBazaar
B
-1

Before to write the archive, move to the good directory :

old_path = os.getcwd()
os.chdir(path)

-> write the archive

After writing the archive move back to old directory :

os.chdir(old_path)
Baalbek answered 2/7, 2019 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.