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.