Downloading files and zipfilesΒΆ

This example shows off how to download a file, and optionally unzip it.

from download import download
import matplotlib.pyplot as plt
import pandas as pd
import os.path as op
from glob import glob
import shutil as sh

Downloading a file simply requires that you have a URL.

url = "https://ndownloader.figshare.com/files/7010681"
path = download(url, "./downloaded/boulder-precip.csv", replace=True)
data = pd.read_csv(path)
ax = data.plot("DATE", "PRECIP")
ax.set(title="Precipitation over time in Boulder, CO")
plt.setp(ax.get_xticklabels(), rotation=45)
plt.tight_layout()
../_images/sphx_glr_plot_download_file_001.png

Out:

Downloading data from https://s3-eu-west-1.amazonaws.com/pfigshare-u-files/7010681/precipboulderaugoct2013.csv (391 bytes)


file_sizes:   0%|                                     | 0.00/391 [00:00<?, ?B/s]
file_sizes: 100%|###############################| 391/391 [00:00<00:00, 882kB/s]
Successfully downloaded file to ./downloaded/boulder-precip.csv

You can optionally unzip the file to the directory of your choice using zipfile=True.

url = "http://www2.census.gov/geo/tiger/GENZ2016/shp/cb_2016_us_county_20m.zip"
path = download(url, "./downloaded/counties/", replace=True, kind="zip")
print(glob(op.join(path, "*")))
sh.rmtree("./downloaded")

plt.show()

Out:

Creating data folder...
Downloading data from https://www2.census.gov/geo/tiger/GENZ2016/shp/cb_2016_us_county_20m.zip?sec_ak_reference=18.b4272417.1587679289.2c16fc4b (1 byte)


file_sizes:   0%|                                    | 0.00/1.00 [00:00<?, ?B/s]
file_sizes: 369kB [00:00, 3.08MB/s]
file_sizes: 880kB [00:00, 5.62MB/s]
Extracting zip file...
Successfully downloaded / unzipped to ./downloaded/counties/
['./downloaded/counties/cb_2016_us_county_20m.prj', './downloaded/counties/cb_2016_us_county_20m.shp', './downloaded/counties/cb_2016_us_county_20m.cpg', './downloaded/counties/cb_2016_us_county_20m.shx', './downloaded/counties/cb_2016_us_county_20m.shp.ea.iso.xml', './downloaded/counties/cb_2016_us_county_20m.dbf', './downloaded/counties/cb_2016_us_county_20m.shp.iso.xml', './downloaded/counties/cb_2016_us_county_20m.shp.xml']

Total running time of the script: ( 0 minutes 3.013 seconds)

Gallery generated by Sphinx-Gallery