Skip to content

Instantly share code, notes, and snippets.

@niftycode
Created May 12, 2021 07:29
Show Gist options
  • Save niftycode/a747648db1b79396b8e4814946a4dba2 to your computer and use it in GitHub Desktop.
Save niftycode/a747648db1b79396b8e4814946a4dba2 to your computer and use it in GitHub Desktop.
Download and unzip a ZIP file using Python
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Download and unzip a ZIP file
Version: 1.0
Python 3.7+
Date created: May 12th, 2021
Date modified: -
"""
import requests
import zipfile
URL = 'https://www.covid19.admin.ch/api/data/20210430-zza0vm8q/downloads/sources-csv.zip'
def fetch_zip_file():
# Try to get the ZIP file
try:
response = requests.get(URL)
except OSError:
print('No connection to the server!')
return None
# check if the request is succesful
if response.status_code == 200:
# Save dataset to file
print('Status 200, OK')
open('sources-csv.zip', 'wb').write(response.content)
else:
print('ZIP file request not successful!.')
return None
def main():
# Get the ZIP file
fetch_zip_file()
# Unzip
with zipfile.ZipFile('sources-csv.zip', 'r') as zip_ref:
zip_ref.extractall()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment