user2437728
user2437728

Reputation: 1

Extracting the extracted with python

I have a zip file containing thousands of mixed .xml and .csv files. I used the following to extract the zip file:

import zipfile

zip = zipfile.ZipFile(r'c:\my.zip')

zip.extractall(r'c:\output')

Now I need to extract the thousands of individual zip files contained in the 'c:\output' folder. I am planning on concatenating just the .csv files into one file. Thank you for the help!

Upvotes: 0

Views: 376

Answers (1)

user2229472
user2229472

Reputation: 509

Try this code :

import zipfile , os

zip = zipfile.ZipFile(r'c:/my.zip')

zip.extractall(r'c:/output')

filelist = []
for name in zip.namelist():
    filelist.append(name)



zip.close()
for i in filelist:


    newzip = zipfile.ZipFile(r'c:/output/'+str(i))
    for file in newzip.namelist():
        if '.csv' in file :
            newzip.extract(file,r'c:/output/')
    newzip.close()
    os.remove(r'c:/output/'+str(i))

Upvotes: 1

Related Questions