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?
Python's zipfile is a standard library module intended to manipulate ZIP files. This file format is a widely adopted industry standard when it comes to archiving and compressing digital data. You can use it to package together several related files.
To compress individual files into a ZIP file, create a new ZipFile object and add the files you want to compress with the write() method. With zipfile. ZipFile() , specify the path of a newly created ZIP file as the first parameter file , and set the second parameter mode to 'w' (write).
You have to use namelist()
and extract()
. Sample considering directories
import zipfile import os.path import os zfile = zipfile.ZipFile("test.zip") for name in zfile.namelist(): (dirname, filename) = os.path.split(name) print "Decompressing " + filename + " on " + dirname if not os.path.exists(dirname): os.makedirs(dirname) zfile.extract(name, dirname)
There's some problem with Vinko's answer (at least when I run it). I got:
IOError: [Errno 13] Permission denied: '01org-webapps-countingbeads-422c4e1/'
Here's how to solve it:
# unzip a file def unzip(path): zfile = zipfile.ZipFile(path) for name in zfile.namelist(): (dirname, filename) = os.path.split(name) if filename == '': # directory if not os.path.exists(dirname): os.mkdir(dirname) else: # file fd = open(name, 'w') fd.write(zfile.read(name)) fd.close() zfile.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With