xyzio

Open a zip archive and iterate through its files with Python

leave a comment »

Open a file compressed as .zip and iterate through its files line by line with Python.


import zipfile

#Path to the zip file
zipfilepath = 'path_to_zip_file'

#Read in zip file
zip = zipfile.ZipFile(zipfilepath)

#Iterate through files in zip file
for zipfilename in zip.filelist:
    
    #Read contents of the file
    filecontents = zip.read(zipfilename)
    
    #Break up contents into list and process
    for line in filecontents.replace('\r\n', '\n').split('\n'):
        print line

Written by M Kapoor

December 24, 2019 at 3:09 am

Posted in Programming

Tagged with ,

Leave a comment