classification
Title: 3.6 ZipFile fails with Path
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: bbayles, berker.peksag, mkleehammer
Priority: normal Keywords:

Created on 2018-07-23 23:17 by mkleehammer, last changed 2018-07-24 03:14 by berker.peksag. This issue is now closed.

Messages (2)
msg322272 - (view) Author: Michael Kleehammer (mkleehammer) * Date: 2018-07-23 23:17
ZipFile is documented to accept "path-like" objects but is failing when I try to create one.  I've distilled it down to this small test:

    import zipfile
    from pathlib import Path

    path = Path('test.zip')
    zf = zipfile.ZipFile(path, 'w')
    zf.writestr('test.txt', 'Hello, Sailor!')
    zf.close()

On macOS I'm getting this error:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    zf.writestr('test.txt', 'Hello, Sailor!')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 1644, in writestr
    with self.open(zinfo, mode='w') as dest:
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 1348, in open
    return self._open_to_write(zinfo, force_zip64=force_zip64)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 1461, in _open_to_write
    self.fp.write(zinfo.FileHeader(zip64))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/zipfile.py", line 721, in write
    n = self.fp.write(data)
AttributeError: 'PosixPath' object has no attribute 'write'

Using str(path) works fine.
msg322275 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2018-07-24 03:14
Thanks for the report! Support for path-like objects was added in 3.6.2. I think you either have 3.6.0 or 3.6.1 installed on your system. Please upgrade it to the latest bugfix release.

With 3.6.6, I got the following results:

>>> import zipfile
>>> from pathlib import Path
>>> path = Path('spam.zip')
>>> zf = zipfile.ZipFile(path, 'w')
>>> zf.writestr('test.txt', 'Hello, Sailor!')
>>> zf.close()

$ ./python -m zipfile -l spam.zip 
File Name                                             Modified             Size
test.txt                                       2018-07-24 06:10:52           14
History
Date User Action Args
2018-07-24 03:14:35berker.peksagsetstatus: open -> closed

nosy: + berker.peksag
messages: + msg322275

resolution: out of date
stage: resolved
2018-07-24 02:40:44bbaylessetnosy: + bbayles
2018-07-23 23:17:13mkleehammercreate