Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 604
user7399815 Avatar asked Oct 12 '25 19:10

user7399815


1 Answers

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])
like image 157
user7399815 Avatar answered Oct 16 '25 06:10

user7399815