Search code examples
pythonautomationjythonpython-2.5jython-2.5

Not able to unzip file from python script


I'm using Python 2.5, and I can't upgrade python version.
Following is what I'm doing.

call(['cp', zipFileName, zipPath]) os.chdir(zipPath) call(['unzip', zipFileName])

In above code I'm getting below error.
unzip: cannot find or open gunzip gunzip.zip or gunzip.ZIP

I did try below code. But still getting same error.

for each in zip.namelist(): if not each.endswith('/'): root, name = split(each) directory = norm(join(path, root)) if not isdir(directory): os.makedirs(directory) file(join(directory, name), 'wb').write(zip.read(each))

Please suggest the workaround if you know any.
Thanks.


Solution

  • Use the zipfile module?

    import zipfile
    
    zf = zipfile.ZipFile(zipFileName)
    zf.extractall(path=zipPath)
    zf.close()
    

    This will extract all files from the zip file into the directory "zipPath".

    Because your Python version is < 2.7.4 please read the warning regarding extract() and extractall() at https://docs.python.org/2/library/zipfile.html#zipfile.ZipFile.extractall .