I'm having a hard time figuring out how to unzip a zip file with 2.4. extract()
is not included in 2.4. I'm restricted to using 2.4.4 on my server.
Can someone please provide a simple code example?
I'm having a hard time figuring out how to unzip a zip file with 2.4. extract()
is not included in 2.4. I'm restricted to using 2.4.4 on my server.
Can someone please provide a simple code example?
Modifying Ovilia's answer so that you can specify the destination directory as well:
You have to use
namelist()
andextract()
. Sample considering directoriesThere's some problem with Vinko's answer (at least when I run it). I got:
Here's how to solve it:
Not fully tested, but it should be okay:
I am testing in Python 2.7.3rc2 and the the
ZipFile.namelist()
is not returning an entry with just the sub directory name for creating a sub directory, but only a list of file names with sub directory, as follows:Thus the check
does not evaluate to
True
at all.So I modified the code to check if the
dirName
exists insidedestDir
and to createdirName
if it does not exist. File is extracted only iffileName
part is not empty. So this should take care of the condition where a directory name can appear inZipFile.namelist()