Skip to content

make shutil.make_archive always accept pathlib objects #85809

Closed

Description

@graingert
mannequin
BPO 41643
Nosy @graingert
PRs
  • bpo-41643: always support pathlib in shutil.make_archive #21962
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2020-08-26.10:55:12.392>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'make shutil.make_archive always accept pathlib objects'
    updated_at = <Date 2020-08-26.11:09:29.339>
    user = 'https://github.com/graingert'

    bugs.python.org fields:

    activity = <Date 2020-08-26.11:09:29.339>
    actor = 'graingert'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2020-08-26.10:55:12.392>
    creator = 'graingert'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41643
    keywords = ['patch']
    message_count = 1.0
    messages = ['375927']
    nosy_count = 1.0
    nosy_names = ['graingert']
    pr_nums = ['21962']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41643'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    Linked PRs

    Activity

    1. graingert commented on Aug 26, 2020

      graingertmannequin
      MannequinAuthor
      >>> shutil.make_archive(pathlib.Path("./foo"), root_dir=pathlib.Path("./foo"), format="zip")
      '/home/graingert/projects/ham/foo.zip'
      
      
      >>> shutil.make_archive(pathlib.Path("./foo"), root_dir=None, format="zip")
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/usr/lib/python3.8/shutil.py", line 1045, in make_archive
          filename = func(base_name, base_dir, **kwargs)
        File "/usr/lib/python3.8/shutil.py", line 912, in _make_zipfile
          zip_filename = base_name + ".zip"
      TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

      see also https://bugs.python.org/issue23241

    2. added
      3.7 (EOL)
      3.8 (EOL)
      stdlib
      type-bug
      on Aug 26, 2020
    3. changed the title [-]make shutil.make_archive always accepts pathlib objects[/-] [+]make shutil.make_archive always accept pathlib objects[/+] on Aug 26, 2020
    4. changed the title [-]make shutil.make_archive always accepts pathlib objects[/-] [+]make shutil.make_archive always accept pathlib objects[/+] on Aug 26, 2020
    5. transferred this issue fromon Apr 10, 2022
    6. 1 remaining item

    7. added
      3.14
      on Jun 4, 2024
    8. ncoghlan commented on Jun 4, 2024

      @ncoghlan
      Contributor

      _make_tarball and _make_zipfile on main still contain the offending concatenation operation that fails when given an os.PathLike instance for base_name rather than str. The actual fix seems straightforward (adding an os.fspath call), but additional regression tests would also be desirable.

      gh-74421 is the still open meta-issue that covers reviewing the state of shutil support for PathLike objects in general.

    9. tadejmagajna commented on Jan 6, 2026

      @tadejmagajna
      Contributor

      This is still generally relevant in 3.14.2:

      >>> from pathlib import Path
      >>> from shutil import make_archive
      >>> make_archive(Path.cwd().resolve(), 'zip')
      TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

      #94844 (comment) mentions the make_archive currently partially supports Pathlib.path albeit only if root_dir is supplied (undocumented). Guido van Rossum's suggestion is we address the issue for case when root_dir is None, albeit with light documentation and brief tests.

      As @ncoghlan suggests above, os.fspath indeed offers a straightforward fix which allowed me to implement the solution including tiny documentation updates and tests.

      Since the linked PR is 5+ years old, would it be okay for me to proceed with a fresh PR introducing path-like object support for make_archive + light tests and documentation?

    10. merwok commented on Jan 7, 2026

      @merwok
      Member

      Please ask on the PR if the author wants to update it

    11. tadejmagajna commented on Jan 8, 2026

      @tadejmagajna
      Contributor

      Thank you. Done, the author closed the issue.

      I shortly plan to open a new PR with the following changes:

      • simple fix for base_name parameter in make_archive using os.fspath (other params are supported already)
      • small documentation change indicating support for path-like objects
      • light tests using FakePath (the favored option to using Pathlib.path in tests directly)

      In case of any concerns about my proposed approach, please let me know.

    12. added
      type-feature
      3.15
      and removed
      type-bug
      3.14
      on Jan 9, 2026
    13. added a commit that references this issue on Jan 10, 2026
    14. added a commit that references this issue on Feb 17, 2026
    15. added a commit that references this issue on Feb 25, 2026
    16. added a commit that references this issue on Feb 28, 2026
    17. added a commit that references this issue on Apr 25, 2026
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.15stdlibtype-feature

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions