How do we unzip a zip file in the file's parent folder in python3.x

I have a requirement to unzip an archive by selecting it at runtime.

I am passing the zip file using HTML input type file in my template and then using ZipFile's function extractall(). This rudimentary arrangement is working, however the unzipped contents are getting saved in the (Django) project's root folder.

I can control where the extracted files get stored by using the path option, which I feel would be a little limiting in the sense that in that case, the folder path will have to be hardcoded. (There are other means of supplying the path information at runtime, but still user intervention would be required.)

The file being an inmemory file, I know the path information will not be available, which otherwise would have been handy.

Is there a way I may save the unzipped file to the same folder as the zipped file is selected from?

Thanks a bucnh.

use django setting

from django.conf import settings

zipfile.extractall(settings.BASE_DIR/{path/to/dir})

not use django setting

from pathlib import Path
path_zipfile = Path({path/to/zipfile})
zipfile = Zipfile(path_zipfile)
zipfile.extractall(path_zipfile.parent / {path/to/dir})

Back to Top