简体   繁体   中英

Extract and rename zip file from specific folder using Python3

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)

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

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM