Unzip files from directory tree

6068
6
Jump to solution
05-19-2015 02:34 AM
Highlighted
Occasional Contributor II

Continusally to Unzip zip files in folders and subfolders with python - i didn't get an answer. i  work with python 2.7.8 and arcmap 10.3

and i don't why this code does not work with python 2.7.8:

  1. import zipfile   
  2. import arcpy,os,os.path,sys 
  3. from arcpy import env 
  4.  
  5. pattern = '*.zip' 
  6. folder = r"C:\Project\layers"  
  7. files_process = [] 
  8. for root,dirs,files in os.walk(r"C:\Project\layers"😞 
  9.     for filenames in files: 
  10.         if filenames == pattern: 
  11.             files_process.append(os.path.join(root, filenames)) 
  12.             zip.extract()
Tags (3)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Occasional Contributor II

Finally, this work's 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, filename.split(".")[0]))

View solution in original post

6 Replies
Highlighted
Occasional Contributor II

You don't have an object named 'zip' that you're calling in the last line.  Read the rest of the link/thread that you posted, especially Dan Patterson​'s reply about researching the module and how to properly use it to extract a zip file.  Throw the code he used there into a for loop, and run it against a directory to unzip all archives.

Highlighted
MVP Esteemed Contributor

Suggest moving this to Python

Highlighted
MVP Esteemed Contributor

Timothy Hales​ an example of the limited moderator ability to move "Developer" threads when posted in the general Discussion section Python development won't get selected unless they navigate.  Again the suggestion to move/alter Developer from level one

Highlighted
Occasional Contributor II

i moved it now to Python.

Highlighted
Occasional Contributor II

Finally, this work's 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, filename.split(".")[0]))

View solution in original post

Highlighted
Occasional Contributor II