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?

somerandomguy95
  • 161
  • 1
  • 3
  • 13

2 Answers2

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.

-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)