Home > database >  Extract and rename zip file from specific folder using Python3
Extract and rename zip file from specific folder using Python3

Time:12-24

I am using python3.10. To unzip a file I have a zip file in folder 'wowo' if I select folder using path and use only file name the code doesn't work. But, when full path filename given it works. I don't want go give full path and file name together. I want to define path saperately.

zipdata = zipfile.ZipFile('/Volumes/MacHD/MYPY/wowo/NST_cm.zip')
    zipinfos = zipdata.infolist()
    for zipinfo in zipinfos:
    zipinfo.filename = 'Nst.csv'
    zipdata.extract(path=path, member=zipinfo)

CodePudding user response:

You could join the two strings in order to form the full filepath.

filepath = os.path.join(path, filename)
zipfile.ZipFile(filepath)

Or I believe the ZipFile function can take a path and file name expression like this

zipfile.ZipFile(path,'filename')

Replacing filename with the name of the file you wish to work with

CodePudding user response:

You can use pathlib and add the path with the filename in the zipfile.zipfile:

import pathlib
path = pathlib.Path('PATH/TO/FOLDER')

zipfile.ZipFile( path / 'filename')

  • Related