Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to read contents of zip file in Python

📅 2013-Oct-03 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ python, zipfile ⬩ 📚 Archive

Reading the contents of files inside a zip file is easy with Python. We can use the zipfile module to achieve this.

Open the zip file using the ZipFile constructor. Get the list of files inside it using infolist() and open each of those files using open(). After that you can read from it just like a normal file.

import zipfile

def read_zip_file(filepath):
    zfile = zipfile.ZipFile(filepath)
    for finfo in zfile.infolist():
        ifile = zfile.open(finfo)
        line_list = ifile.readlines()
        print line_list

Tried with: Python 2.7.3