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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: