How to Open a ZIP file in Python

The easiest way to open a zip file in Python and read its contents is to use the zipfile package. Once this package is imported we can create a new zipfile object, open it with the Python .open() function then use the .read method on that object.

 

import zipfile

zf = zipfile.ZipFile('sample.zip')

file = zf.open('sample.txt')
   
print(file.read)
Some content.

Related Tutorials

 thumbnail

Unzip Files in Python

June 30, 2021
 thumbnail

The Python zip() Method

November 04, 2020
 thumbnail

The Python zip() Function

October 04, 2020