Title: shutil.make_archive should recognize extensions in filenames
Type: enhancement Stage: resolved
Components: Versions: Python 2.7
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: andreas-h, iritkatriel, tiwilliam
Priority: normal Keywords:

Created on 2013-10-10 08:50 by andreas-h, last changed 2021-06-18 10:22 by iritkatriel. This issue is now closed.

Messages (3)
msg199373 - (view) Author: Andreas Hilboll (andreas-h) Date: 2013-10-10 08:50
shutil.make_archive should be able to automatically determine the desired *format* from the given filename. It would make life easier, because the programmer wouldn't need to strip the extension from the filename before passing it to make_archive. I'm think of something along the lines of

        if base_path.lower().endswith(".zip"):
            fmt = "zip"
            base_path = base_path[:-4]
        elif base_path.lower().endswith(".tar.gz") or base_path.lower().endswith(".tgz"):
            fmt = "gztar"
            base_path = base_path[:-7]
        elif base_path.lower().endswith(".tar.bz2"):
            fmt = "bztar"
            base_path = base_path[:-8]
        elif base_path.lower().endswith(".tar"):
            fmt = "tar"
            base_path = base_path[:-4]
msg217559 - (view) Author: William Tis├Ąter (tiwilliam) * Date: 2014-04-29 22:35
I'm not sure if this is a suitable feature in `make_archive()`, it would only introduce a more expensive and ugly lookup. Using this method with a pre-defined filename including extension must be rare. If you really want this behaviour, I would prefer having this helper instead:

    def make_archive(filename, **kwargs):
        for fmt, ext, info in shutil.get_unpack_formats():
            if not filename.lower().endswith(tuple(ext)):
            return shutil.make_archive(filename[:-len(ext)], fmt, **kwargs)
        raise ValueError("Unknown archive format: %s" % filename)

Not sure what version you are using, `get_unpack_formats()` got introduced in 3.2.
msg396041 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-06-18 10:22
I agree with William that it is better to do this in a helper function than to further complicate make_archive.
Date User Action Args
2021-06-18 10:22:46iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg396041

resolution: rejected
stage: resolved
2014-04-29 22:35:47tiwilliamsetnosy: + tiwilliam
messages: + msg217559
2013-10-10 08:50:37andreas-hcreate