Shutil make_archive, full path inside archive

There is something I haven’t understood on how to use shutil.make_archive.

Example:

While at /path/to/, I do

import shutil
shutil.make_archive(base_name='text.txt', format='zip', base_dir='/path/to/text.txt')

The result is a file text.txt.zip located at /path/to/text.txt.zip. Fine.

In the archive, the entire path /path/to/text.txt has been replicated.

Question 1: How should shutil.make_archive be used to only have text.txt inside the zip file?

Note: Instead of just a file text.txt I might also want to archive a directory, but I also do not want the entire path to it inside the zip file.


In the documentation it says

Changed in version 3.10.6: This function is now made thread-safe during creation of standard .zip and tar archives.

Question 2: What is a not “standard .zip […] archive”? Am I, in the example above, in a standard case?


I get the same effect with

import shutil
shutil.make_archive(base_name='text.txt', format='zip', base_dir='/path/to/text.txt', root_dir='/path/to')

In this example in the documentation they get the directory tree in the archive, but starting from root_dir.

Have you tried setting root_dir ='/path/to'?

What is a not “standard .zip […] archive”?

format= “gztar”, “bztar”, or “xztar”.

Yes, let me add it to the example. I got the same effect, the entire path inside the archive.

Interestingly, in this example in the docs, they get the path but starting from the values of root_dir onward.

The problem is that base_dir needs to be " must be given relative to root_dir".

So, I should do something like

import shutil
shutil.make_archive(base_name='text.txt', format='zip', base_dir='', root_dir='/path/to')

and then inside text.txt.zip I will get only text.txt.