Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Forked from kalkulus/unzip_all.py
Created March 15, 2019 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/c2778e907f7d4d3de53d71217591eebe to your computer and use it in GitHub Desktop.
Save ryanbekabe/c2778e907f7d4d3de53d71217591eebe to your computer and use it in GitHub Desktop.
PYTHON: unzip all files in a directory
#!/usr/bin/python
import os, zipfile
for filename in os.listdir("."):
if filename.endswith(".zip"):
print filename
name = os.path.splitext(os.path.basename(filename))[0]
if not os.path.isdir(name):
try:
zip = zipfile.ZipFile(filename)
os.mkdir(name)
zip.extractall(path=name)
except zipfile.BadZipfile, e:
print "BAD ZIP: "+filename
try:
os.remove(filename)
except OSError as e: # this would be "except OSError, e:" before Python 2.6
if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory
raise # re-raise exception if a different error occured
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment