Skip to content

Commit bd4e9e0

Browse files
akuchlingMariatta
authored andcommitted
bpo-6519: Improve Python Input Output Tutorial (GH-2143)
Move up the discussion about 'with' keyword, so it appears earlier in the document.
1 parent 81f67b6 commit bd4e9e0

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

Doc/tutorial/inputoutput.rst

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,35 @@ to file data is fine for text files, but will corrupt binary data like that in
262262
:file:`JPEG` or :file:`EXE` files. Be very careful to use binary mode when
263263
reading and writing such files.
264264

265+
It is good practice to use the :keyword:`with` keyword when dealing
266+
with file objects. The advantage is that the file is properly closed
267+
after its suite finishes, even if an exception is raised at some
268+
point. Using :keyword:`with` is also much shorter than writing
269+
equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
270+
271+
>>> with open('workfile') as f:
272+
... read_data = f.read()
273+
>>> f.closed
274+
True
275+
276+
If you're not using the :keyword:`with` keyword, then you should call
277+
``f.close()`` to close the file and immediately free up any system
278+
resources used by it. If you don't explicitly close a file, Python's
279+
garbage collector will eventually destroy the object and close the
280+
open file for you, but the file may stay open for a while. Another
281+
risk is that different Python implementations will do this clean-up at
282+
different times.
283+
284+
After a file object is closed, either by a :keyword:`with` statement
285+
or by calling ``f.close()``, attempts to use the file object will
286+
automatically fail. ::
287+
288+
>>> f.close()
289+
>>> f.read()
290+
Traceback (most recent call last):
291+
File "<stdin>", line 1, in <module>
292+
ValueError: I/O operation on closed file
293+
265294

266295
.. _tut-filemethods:
267296

@@ -354,27 +383,6 @@ to the very file end with ``seek(0, 2)``) and the only valid *offset* values are
354383
those returned from the ``f.tell()``, or zero. Any other *offset* value produces
355384
undefined behaviour.
356385

357-
358-
When you're done with a file, call ``f.close()`` to close it and free up any
359-
system resources taken up by the open file. After calling ``f.close()``,
360-
attempts to use the file object will automatically fail. ::
361-
362-
>>> f.close()
363-
>>> f.read()
364-
Traceback (most recent call last):
365-
File "<stdin>", line 1, in <module>
366-
ValueError: I/O operation on closed file
367-
368-
It is good practice to use the :keyword:`with` keyword when dealing with file
369-
objects. This has the advantage that the file is properly closed after its
370-
suite finishes, even if an exception is raised on the way. It is also much
371-
shorter than writing equivalent :keyword:`try`\ -\ :keyword:`finally` blocks::
372-
373-
>>> with open('workfile', 'r') as f:
374-
... read_data = f.read()
375-
>>> f.closed
376-
True
377-
378386
File objects have some additional methods, such as :meth:`~file.isatty` and
379387
:meth:`~file.truncate` which are less frequently used; consult the Library
380388
Reference for a complete guide to file objects.

0 commit comments

Comments
 (0)