Skip to content

Commit c89b221

Browse files
andresdelfinoorsenthil
authored andcommitted
Remove to-be-deprecated urllib.request.urlretrieve function reference (#6454)
1 parent b8e21f1 commit c89b221

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Doc/howto/urllib2.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,20 @@ The simplest way to use urllib.request is as follows::
5656
with urllib.request.urlopen('http://python.org/') as response:
5757
html = response.read()
5858

59-
If you wish to retrieve a resource via URL and store it in a temporary location,
60-
you can do so via the :func:`~urllib.request.urlretrieve` function::
59+
If you wish to retrieve a resource via URL and store it in a temporary
60+
location, you can do so via the :func:`shutil.copyfileobj` and
61+
:func:`tempfile.NamedTemporaryFile` functions::
6162

63+
import shutil
64+
import tempfile
6265
import urllib.request
63-
local_filename, headers = urllib.request.urlretrieve('http://python.org/')
64-
html = open(local_filename)
66+
67+
with urllib.request.urlopen('http://python.org/') as response:
68+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
69+
shutil.copyfileobj(response, tmp_file)
70+
71+
with open(tmp_file.name) as html:
72+
pass
6573

6674
Many uses of urllib will be that simple (note that instead of an 'http:' URL we
6775
could have used a URL starting with 'ftp:', 'file:', etc.). However, it's the

0 commit comments

Comments
 (0)