`zipfile.ZipFile.writestr()` raises `AttributeError` instead of accepting `os.PathLike` objects (e.g. `pathlib.Path`)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug report
Bug description:
Unlike ZipFile.write() (which accepts os.PathLike objects such as pathlib.Path for both its filename and arcname arguments), ZipFile.writestr() fails when passed a PathLike object as its zinfo_or_arcname parameter.
Instead of converting it to a string using os.fspath() (as expected by modern standard library conventions), it attempts to call .find(chr(0)) directly on the path object, resulting in an immediate AttributeError.
Repro:
import zipfile
import pathlib
import io
z = zipfile.ZipFile(io.BytesIO(), 'w')
# Passes a pathlib.Path instead of a raw string
z.writestr(pathlib.Path('foo.txt'), b'hello')
Traceback:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../zipfile/__init__.py", line 2020, in writestr
zinfo = ZipInfo(zinfo_or_arcname)._for_archive(self)
^^^^^^^^^^^^^^^^^^^^^^^^^
File ".../zipfile/__init__.py", line 455, in __init__
filename = _sanitize_filename(filename)
File ".../zipfile/__init__.py", line 410, in _sanitize_filename
null_byte = filename.find(chr(0))
^^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'find'
CPython versions tested on:
3.14
Operating systems tested on:
macOS
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in zipfile/init.py at ZipFile.writestr(), ZipInfo.init(), and _sanitize_filename(), then run the pathlib.Path reproduction from the issue. Done means writestr() accepts os.PathLike objects such as pathlib.Path without raising AttributeError while retaining existing string behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100