'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?
Solution 1:[1]
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])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | algorithms |