简体   繁体   中英

Python ZipFile return extracted file path and name

I have this current code to unzip the contents of archive to extract_dir . However, I cannot figure out how to get the extracted file path & name of the extracted file.

if archive.endswith((".zip")):
    zip_ref = zipfile.ZipFile(archive, 'r')
    zip_ref.extract(extract_dir)
    zip_ref.close()

For example, if the archive is called test.zip and ZipFile extracts the contents test.exe I want get C:/windows/users/admin/downloads/test.exe in to a variable?

EDIT: Sorry I wasn't clear, in the source code for zipfile targetpath is returned I am wondering how I can get this?

Here is the solution, I can't accept the answer for 2 days though.

if archive.endswith((".zip")):
    print "example.jpg"
    zip_ref = zipfile.ZipFile(archive, 'r')
    extracted = zip_ref.namelist()
    zip_ref.extractall(extract_dir)
    zip_ref.close()
    extracted_file = os.path.join(extract_dir, extracted[0])

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