Mercurial > cpython-lru_cache-weakref
comparison Doc/tutorial/errors.rst @ 80758:29627bd5b333 2.7
Drop double newlines printed in some file iteration examples.
Patch by Steven Kryskalla.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Sat, 08 Dec 2012 18:01:27 +0200 |
| parents | b873afe640e2 |
| children |
comparison
equal
deleted
inserted
replaced
| 80747:57f5771de27d | 80758:29627bd5b333 |
|---|---|
| 395 is no longer needed, regardless of whether or not the operation using the object | 395 is no longer needed, regardless of whether or not the operation using the object |
| 396 succeeded or failed. Look at the following example, which tries to open a file | 396 succeeded or failed. Look at the following example, which tries to open a file |
| 397 and print its contents to the screen. :: | 397 and print its contents to the screen. :: |
| 398 | 398 |
| 399 for line in open("myfile.txt"): | 399 for line in open("myfile.txt"): |
| 400 print line | 400 print line, |
| 401 | 401 |
| 402 The problem with this code is that it leaves the file open for an indeterminate | 402 The problem with this code is that it leaves the file open for an indeterminate |
| 403 amount of time after the code has finished executing. This is not an issue in | 403 amount of time after the code has finished executing. This is not an issue in |
| 404 simple scripts, but can be a problem for larger applications. The | 404 simple scripts, but can be a problem for larger applications. The |
| 405 :keyword:`with` statement allows objects like files to be used in a way that | 405 :keyword:`with` statement allows objects like files to be used in a way that |
| 406 ensures they are always cleaned up promptly and correctly. :: | 406 ensures they are always cleaned up promptly and correctly. :: |
| 407 | 407 |
| 408 with open("myfile.txt") as f: | 408 with open("myfile.txt") as f: |
| 409 for line in f: | 409 for line in f: |
| 410 print line | 410 print line, |
| 411 | 411 |
| 412 After the statement is executed, the file *f* is always closed, even if a | 412 After the statement is executed, the file *f* is always closed, even if a |
| 413 problem was encountered while processing the lines. Other objects which provide | 413 problem was encountered while processing the lines. Other objects which provide |
| 414 predefined clean-up actions will indicate this in their documentation. | 414 predefined clean-up actions will indicate this in their documentation. |
| 415 | 415 |
