diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2015-04-12 10:53:33 (GMT) |
---|---|---|
committer | Berker Peksag <berker.peksag@gmail.com> | 2015-04-12 10:53:33 (GMT) |
commit | 556e08e9b2016121e0984039c7087f52ba064297 (patch) | |
tree | 0c3dcc73147a7887a12a9d3ac75c9213fc12c914 /Doc/tutorial | |
parent | 48a724fa33143bcbab2228058d3f59e1813bd49c (diff) | |
parent | 9575e1891ff533318f6dd72ab6f6bd0f9a042014 (diff) | |
download | cpython-556e08e9b2016121e0984039c7087f52ba064297.zip cpython-556e08e9b2016121e0984039c7087f52ba064297.tar.gz cpython-556e08e9b2016121e0984039c7087f52ba064297.tar.bz2 |
Issue #12955: Change the urlopen() examples to use context managers where appropriate.
Patch by Martin Panter.
Diffstat (limited to 'Doc/tutorial')
-rw-r--r-- | Doc/tutorial/stdlib.rst | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst index 954ef44..598859d 100644 --- a/Doc/tutorial/stdlib.rst +++ b/Doc/tutorial/stdlib.rst @@ -153,10 +153,11 @@ protocols. Two of the simplest are :mod:`urllib.request` for retrieving data from URLs and :mod:`smtplib` for sending mail:: >>> from urllib.request import urlopen - >>> for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'): - ... line = line.decode('utf-8') # Decoding the binary data to text. - ... if 'EST' in line or 'EDT' in line: # look for Eastern Time - ... print(line) + >>> with urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl') as response: + ... for line in response: + ... line = line.decode('utf-8') # Decoding the binary data to text. + ... if 'EST' in line or 'EDT' in line: # look for Eastern Time + ... print(line) <BR>Nov. 25, 09:43:32 PM EST |