Issue34202
Created on 2018-07-23 23:17 by mkleehammer, last changed 2022-04-11 14:59 by admin. 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) * ![]() |
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 |
| 2022-04-11 14:59:03 | admin | set | github: 78383 |
| 2018-07-24 03:14:35 | berker.peksag | set | status: open -> closed nosy:
+ berker.peksag resolution: out of date |
| 2018-07-24 02:40:44 | bbayles | set | nosy:
+ bbayles |
| 2018-07-23 23:17:13 | mkleehammer | create | |
