.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_gallery_plot_download_file.py:
Downloading files and zipfiles
------------------------------
This example shows off how to download a file, and optionally unzip it.
.. code-block:: default
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.
.. code-block:: default
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()
.. image:: /gallery/images/sphx_glr_plot_download_file_001.png
:class: sphx-glr-single-img
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
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``.
.. code-block:: default
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()
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
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']
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 3.013 seconds)
.. _sphx_glr_download_gallery_plot_download_file.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_download_file.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_download_file.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_