What command to use instead of urllib.request.urlretrieve?

Marcus picture Marcus · Feb 23, 2013 · Viewed 16.6k times · Source

I'm currently writing a script that downloads a file from a URL

import urllib.request
urllib.request.urlretrieve(my_url, 'my_filename')

According to the docs, urllib.request.urlretrieve is a legacy interface and might become deprecated, therefore I would like to avoid it so I don't have to rewrite this code in the near future.

I'm unable to find another interface like download(url, filename) in standard libraries. If urlretrieve is considered a legacy interface in Python 3, what is the replacement?

Answer

Jon-Eric picture Jon-Eric · Feb 23, 2013

Deprecated is one thing, might become deprecated at some point in the future is another.

If it suits your needs, I'd continuing using urlretrieve.

That said, you can do without it:

from urllib.request import urlopen
from shutil import copyfileobj

with urlopen(my_url) as in_stream, open('my_filename', 'wb') as out_file:
    copyfileobj(in_stream, out_file)