How to download a pdf or text file from a url using python ?

Example of how to download a file from a url using python using mpython module urllib and urlretrieve function.

Using python 3

Let's take for example the file available on

>>> import urllib.request
>>> file_url = ''
>>> file_output_name = 'file_downloaded.pdf'
>>> urllib.request.urlretrieve(file_url, file_output_name)

Using python 2

Another example in python 2

>>> import urllib
>>> urllib.urlretrieve('', "st-intro.pdf")

See also