zip - Python unzip a file and check if file exists -


I am starting a workflow that includes unziping data every morning, the zip will look like this:

  zip_download.zip set 1 2014 February 17 Image_1.png Image_2.png Set 2 2014 February 14 Image_1.png Image_2.png Example File Path: zip_download.zip I open these files daily and basically What is required for Python to create a database in Windows folders / set 1/2014 / February / 14th / image_1.png  

Sometimes the day before There can be data for that and that folder may already exist so I need to handle that error.

Even I have so far:

  import zipfile import OS target_location = r'C: / new_filestructure 'file_download = r'C: / user . For Mike / Download Zipfile.zipfile (target_zip) with /download1.zip as' zip_file: zip_file.namelist (for member): try: os.makedirs (os.path.join (target_location + r '/' Excluding os.path.dirname (member))): (OSSError, WindowsError) print os.path.basename (member) print os.path.dirname (member)  

this It will go fine and what I want to print (just to check if it is running through all the files), but it will not create a single folder. If I try / except I say an error that the directory already exists when it does not clearly.

Any thoughts?

The main problem in your code was that you have errors when more than one file was in the same directory. For, "Set 1/2014 / February / 17th" directory has been created twice: once for Image_1.png and then for Image_2.png.

You also had a strange thing

  file_download  

  target_zip  

was the code: zipfile import OS target_location = r'new_filestructure 'target_zip = r'zip.zip' zipfile.ZipFile (target_zip) with zip_file: member for zip_file.namelist (): if os . Path.exists (target_location + r '/' + member) or os.path.isfile (target_location + r '/' + member): Print 'error:', member, 'exists.' Other: zip_file.extract (member, target_location)


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -