Python Language Working with ZIP archives Opening Zip Files

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To start, import the zipfile module, and set the filename.

import zipfile
filename = 'zipfile.zip'

Working with zip archives is very similar to working with files, you create the object by opening the zipfile, which lets you work on it before closing the file up again.

zip = zipfile.ZipFile(filename)
print(zip)
# <zipfile.ZipFile object at 0x0000000002E51A90>
zip.close()

In Python 2.7 and in Python 3 versions higher than 3.2, we can use the with context manager. We open the file in "read" mode, and then print a list of filenames:

with zipfile.ZipFile(filename, 'r') as z:
    print(zip)
    # <zipfile.ZipFile object at 0x0000000002E51A90>


Got any Python Language Question?