Last active
June 8, 2022 11:58
-
-
Save kalkulus/8049212 to your computer and use it in GitHub Desktop.
PYTHON: unzip all files in a directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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
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: '.............'