Skip to content

Instantly share code, notes, and snippets.

@rayou
Forked from cesandoval/unzip.py
Last active December 15, 2015 19:19
Show Gist options
  • Select an option

  • Save rayou/5310692 to your computer and use it in GitHub Desktop.

Select an option

Save rayou/5310692 to your computer and use it in GitHub Desktop.
Python: unzip.py
import zipfile
import os
#Extracts files on a zip file into a given directory, if no directory is given,
#it extracts the files into the zip file directory.
def unzip_file(zip_file, my_path=None):
zip_file = zipfile.ZipFile(zip_file) #open the zip file
if my_path != None: #if directory is given, and doesn't exist, create directory.
if not os.path.isdir(my_path):
os.makedirs(my_path)
for f in zip_file.namelist(): #extract all the files into the given directory
zip_file.extract(f, my_path=None)
else: #if no directory is given, extract the files into the zipfiles's directory.
for f in zip_file.namelist():
zip_file.extract(f)
zip_file = 'C:\Users\carlos\Desktop\python\Json_File.zip' #set the zipfile's directory.
#my_path = 'C:\Users\carlos\Desktop\python\Json_File' #set the directory to extract files.
unzip_file(zip_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment