In this tutorial, you will learn how to download files from the web using wget Python modules and unzip it.
Run pip install wget if you have not yet install wget on your system. Below function will create a new local directory if not exit, download the web file then unzip it.
import wget
import os
def download_web_file_locally (URL, LOCAL_DIR_FILE, WORKING_DIR):
# CREATE LOCALL DIRECTORY IF NOT EXISTS
if not os.path.exists(WORKING_DIR):
os.makedirs(WORKING_DIR)
# START DOWNLOADING THE FILE FROM THE WEB
DOWNLOAD_FILE = WORKING_DIR + LOCAL_DIR_FILE
wget.download(URL, DOWNLOAD_FILE)
# UNZIP THE ZILE TO A DIFFERENT DIRECTORY NAME
print('unzipping gtfs file')
with zipfile.ZipFile(DOWNLOAD_FILE,'r') as zip_ref:
zip_ref.extractall(WORKING_DIR)
Now, let’s execute the function to download and unzip the file.
URL = "YOU WEB URL"
LOCAL_DIR_FILE = "YOUR LOCAL DIRECTORY NAME"
WORKING_DIR = "YOUR WORKING DIRECTORY NAME" # WHERE TO UNZIP THE FILE
def download_web_file_locally (URL, LOCAL_DIR_FILE, WORKING_DIR)