Working with zip files in Python

Since I started working with computers, my favorite file format was ‘zip’, popularized by the Windows PKZIP program. The idea of compressing data into shorter versions of themselves was a fascinating idea.

Then, I was ignorant of the mathematical foundations of information theory, so it seemed like magic. I spent umpteen hours playing with file compression – exploring the various options available and seeing how much further I could compress a file. Now compression is a normal part of any computer language library. So when learning Python I first started exploring the compression library.

The Python ‘zipfile’ module can be used to read and write zip files. A simple exampe to view the contents of a zip file is given below.

from __future__ import print_function
import zipfile
 
with zipfile.ZipFile('python.zip','r') as zf:
        print(zf.namelist())
 
# Returns a list of file names in the zip file
# ['test.py', 'test2.py', 'test3.py']

We can test if file is a zip file regardless the extension.

>>zipfile.is_zipfile('python.zip')
>>True

Also we can test for multiple files.

from __future__ import print_function
import zipfile
 
for filename in ['python.zip', 'test.py', 'test2.py']:
        print('{:>20}  {}'.format(filename, zipfile.is_zipfile(filename)))

Which will return.

python.zip  True
   test.py  False
  test2.py  False

To extract all the files from the zip archive do the following. This will extract all the file to the current directory.

>>import zipfile
>>zp = zipfile.ZipFile('python.zip')
>>zp.extractall()

You can also specify the path.

>>zp.extractall('./test)

We can also extract a single file from the zip archive by specifying the file name.

>>zp.extract('test.py')

This is just a short overview of the zipfile module. In future posts we will go into other details.

Leave a Reply

Your email address will not be published. Required fields are marked *