Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unzip a file and check if file exists

Tags:

python

zip

I am starting a workflow that involves unzipping data every morning. The zip would look something like this:

zip_download.zip
 Set 1
   2014
    Feb
     17th
      Image_1.png
      Image_2.png
 Set 2
   2014
    Feb
     14th
      Image_1.png
      Image_2.png

Example file path: zip_download.zip/set 1/2014/Feb/14th/Image_1.png

What I need is for Python to unzip these files daily and basically build a database in windows folders. Sometimes there could be data for previous days and that folder may already exist so I need that error handling.

Here is what I have so far:

import zipfile
import os

target_location = r'C:/new_filestructure'
file_download = r'C:/Users.Mike/Downloads/download1.zip'

with zipfile.ZipFile(target_zip) as zip_file:
    for member in zip_file.namelist():
        try:
            os.makedirs(os.path.join(target_location + r'/' + os.path.dirname(member)))
        except:
            (OSSError, WindowsError)
    print os.path.basename(member)
    print os.path.dirname(member)

This will run fine and print what i want (just testing to see if it is going through all the files) but it will not create a single folder. If I take out the try/ except I will get an error saying that the directory already exists when it clearly doesn't.

Any ideas?

like image 542
Michael Avatar asked Oct 17 '25 10:10

Michael


1 Answers

The main problem in your code was that you would cause errors when multiple files were in the same directory. For example, the directory "Set 1/2014/Feb/17th" is created twice: once for Image_1.png and again for Image_2.png.

You also had a weird thing where

file_download

should have been

target_zip

The code I ended up with was:

import zipfile
import os

target_location = r'new_filestructure'
target_zip = r'zip.zip'

with zipfile.ZipFile(target_zip) as zip_file:
    for member in zip_file.namelist():
        if os.path.exists(target_location + r'/' + member) or os.path.isfile(target_location + r'/' + member):
            print 'Error: ', member, ' exists.'
        else:
            zip_file.extract(member, target_location)
like image 183
user3264405 Avatar answered Oct 20 '25 01:10

user3264405



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!