Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip zip files in folders and subfolders

Tags:

python

unzip

I try to unzip 150 zip files. All the zip files as different names, and they all spread in one big folder that divided to a lot of sub folders and sub sub folders.i want to extract each archive to separate folder with the same name as the original zip file name and also in the same place as the original zip file . my code is:

import zipfile    
import os,os.path,sys  

pattern = '*.zip'  
folder = r"C:\Project\layers"   
files_process = []  
for root,dirs,files in os.walk(r"C:\Project\layers"):  
    for filenames in files:  
        if filenames == pattern:  
            files_process.append(os.path.join(root, filenames))  
            zip.extract() 

After i run the code nothing happened. Thanks in advance for any help on this.

like image 412
newGIS Avatar asked Feb 05 '15 08:02

newGIS


1 Answers

UPDATE:

Finally, this code worked for me:

import zipfile,fnmatch,os

rootPath = r"C:\Project"
pattern = '*.zip'
for root, dirs, files in os.walk(rootPath):
    for filename in fnmatch.filter(files, pattern):
        print(os.path.join(root, filename))
        zipfile.ZipFile(os.path.join(root, filename)).extractall(os.path.join(root, os.path.splitext(filename)[0]))
like image 89
newGIS Avatar answered Sep 28 '22 06:09

newGIS