Last active
October 14, 2015 16:52
-
-
Save Mattamorphic/b1f497571e558afe435d to your computer and use it in GitHub Desktop.
Unzip files and return a list of the extracted files in Python
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
def unzip_file(self, path): | |
''' | |
Using a file path, unzip the file in to a directory of files returning a list of files in that folder | |
:param path, a string representing the location of the zip file (path/to/zip.zip) | |
:rtype list of files in decompressed folder | |
''' | |
import os | |
import zipfile | |
folder, ext = os.path.splitext(path) | |
if not os.path.exists(folder): | |
os.makedirs(folder) | |
zip_ref = zipfile.ZipFile(path, 'r') | |
zip_ref.extractall(folder) | |
zip_ref.close() | |
return [ | |
'{}/{}'.format(folder, f) | |
for f in os.listdir(folder) | |
if os.path.isfile(os.path.join(folder, f)) | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment