I've looked through the examples out there and don't seem to find one that fits.
Looking to unzip a file in-memory to a temporary directory using Python mkdtemp()
.
Something like this feels intuitive, but I can't find the correct syntax:
import zipfile
import tempfile
zf = zipfile.Zipfile('incoming.zip')
with tempfile.mkdtemp() as tempdir:
zf.extractall(tempdir)
# do stuff on extracted files
But this results in:
AttributeError Traceback (most recent call last)
<ipython-input-5-af39c866a2ba> in <module>
1 zip_file = zipfile.ZipFile('incoming.zip')
2
----> 3 with tempfile.mkdtemp() as tempdir:
4 zip_file.extractall(tempdir)
AttributeError: __enter__
with
andas
. It returns the path name as a string and not a context manager. Read this -> Python: Why am I receiving an AttributeError: __enter__ – Incautiousmkdtemp()
does not return a context manager. It returns a path. You have to handle it manually. And it also does not work in-memory except your temp directory is on a RAM drive. – Evocationtempfile.TemporaryDirectory
I reckon as it comes with context manager. But really depending on what you are trying to do, there is a good chance you do not need to unpack the zip first in order to do that. – Kitchenware