Extract Zip files to one folder

7263
6
06-02-2015 11:01 PM
Yaron_YosefCohen
Occasional Contributor II

Hi everyone,

Continuously to Unzip files from directory tree ,when i try to  extract zip files that contain files with same names in it to one folder , some files got lost because duplication names. I use this code (i work with python 2.7.8 and arcmap 10.3):

import zipfile,fnmatch,os

# EXTRACT ALL ZIP FILES IN DIRECTORY TREE TO ONE SPECIFIC FOLDER 

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))
        outpath = r"C:\Project\gis\layers\taba\new"  
        print root
        print filename
        zipfile.ZipFile(os.path.join(root, filename)).extractall(r"C:\Project\gis\layers\taba\new")

I don't know how to overcome that issue

Thank for any help

Tags (2)
0 Kudos
6 Replies
SepheFox
Frequent Contributor

Under your outpath assignment put an if statement.

for root, dirs, files in os.walk(rootPath): 

    for filename in fnmatch.filter(files, pattern): 

        print(os.path.join(root, filename)) 

        outpath = r"C:\Project\gis\layers\taba\new"   

    for files in arcpy.ListFiles(outpath😞

            If files <> filename:

                print root 

                print filename 

                zipfile.ZipFile(os.path.join(root, filename)).extractall(r"C:\Project\gis\layers\taba\new")

Or something like that anyway

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

I get :

1.jpg2.jpg

0 Kudos
DanPatterson_Retired
MVP Emeritus

your if is capitalized

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

thanks-i've changed that now- nothing happened

>>> ================================ RESTART ================================
>>>
>>>
0 Kudos
JeffWard
Occasional Contributor III

It completed, but your your expression for your if statement was never true, so nothing printed.  It looks like you are reassigning your variable "files" multiple times?  Try a different variable name other than "files" for your third for loop.

Jeff Ward
Summit County, Utah
DanPatterson_Retired
MVP Emeritus

You are really getting muddled in retaining your paths.  Might I suggest that you do the following

  1. collect the filenames into a list first
  2. remove or rename any duplicates
  3. process the filenames in the previous step last

putting all this into a function will help.  Have a look a my blog post on finding files with a particular file extension.

Before I forget ... # 4 ... Where did that script go?