Issue
I'm currently writing a script that downloads a file from a URL
import urllib.request
urllib.request.urlretrieve(my_url, 'my_filename')
The docs urllib.request.urlretrieve
state:
The following functions and classes are ported from the Python 2 module urllib (as opposed to urllib2). They might become deprecated at some point in the future.
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?
Solution
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 use shutil.copyfileobj
:
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)
Answered By - Jon-Eric
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.