Search code examples
python-3.xzipshutil

shutil.make_archive not zipping to correct destination


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?


Solution

  • 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.