Skip to content

Instantly share code, notes, and snippets.

@kalkulus
Last active June 8, 2022 11:58
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save kalkulus/8049212 to your computer and use it in GitHub Desktop.
Save kalkulus/8049212 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
@bktbunu
Copy link

bktbunu commented Mar 19, 2021

I have tried executing the code saving it as zipextractor.py. But it generated the following error although I mentioned the path correctly.

File "zipextractor.py", line 9, in
zip = zipfile.ZipFile(filename)
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\zipfile.py", line 1204, in init
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: '.............'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment