Skip to content

Instantly share code, notes, and snippets.

@Mattamorphic
Last active October 14, 2015 16:52
Show Gist options
  • Save Mattamorphic/b1f497571e558afe435d to your computer and use it in GitHub Desktop.
Save Mattamorphic/b1f497571e558afe435d to your computer and use it in GitHub Desktop.
Unzip files and return a list of the extracted files in Python
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