diff options
author | Georg Brandl <georg@python.org> | 2010-07-31 20:08:15 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-07-31 20:08:15 (GMT) |
commit | 6cb7b6593ee03aedebae1bc9bd0f76912efa275a (patch) | |
tree | 30793f9f226d6e647e4bd3a9c6d14d3f24a9f775 /Doc | |
parent | e42a59daec40d1238a0189cde735f97aebe2d0b7 (diff) | |
download | cpython-6cb7b6593ee03aedebae1bc9bd0f76912efa275a.zip cpython-6cb7b6593ee03aedebae1bc9bd0f76912efa275a.tar.gz cpython-6cb7b6593ee03aedebae1bc9bd0f76912efa275a.tar.bz2 |
#1286: allow using fileinput.FileInput as context manager.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/fileinput.rst | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/Doc/library/fileinput.rst b/Doc/library/fileinput.rst index d98a198..eac324d 100644 --- a/Doc/library/fileinput.rst +++ b/Doc/library/fileinput.rst @@ -24,7 +24,7 @@ as the first argument to :func:`.input`. A single file name is also allowed. All files are opened in text mode by default, but you can override this by specifying the *mode* parameter in the call to :func:`.input` or -:class:`FileInput()`. If an I/O error occurs during opening or reading a file, +:class:`FileInput`. If an I/O error occurs during opening or reading a file, :exc:`IOError` is raised. If ``sys.stdin`` is used more than once, the second and further use will return @@ -54,6 +54,16 @@ The following function is the primary interface of this module: during iteration. The parameters to this function will be passed along to the constructor of the :class:`FileInput` class. + The :class:`FileInput` instance can be used as a context manager in the + :keyword:`with` statement. In this example, *input* is closed after the + :keyword:`with` statement is exited, even if an exception occurs:: + + with fileinput.input(files=('spam.txt', 'eggs.txt')) as input: + process(input) + + .. versionchanged:: 3.2 + Can be used as a context manager. + The following functions use the global state created by :func:`fileinput.input`; if there is no active state, :exc:`RuntimeError` is raised. @@ -132,13 +142,23 @@ available for subclassing as well: *filename* and *mode*, and returns an accordingly opened file-like object. You cannot use *inplace* and *openhook* together. + A :class:`FileInput` instance can be used as a context manager in the + :keyword:`with` statement. In this example, *input* is closed after the + :keyword:`with` statement is exited, even if an exception occurs:: + + with FileInput(files=('spam.txt', 'eggs.txt')) as input: + process(input) + + .. versionchanged:: 3.2 + Can be used as a context manager. + -**Optional in-place filtering:** if the keyword argument ``inplace=1`` is passed -to :func:`fileinput.input` or to the :class:`FileInput` constructor, the file is -moved to a backup file and standard output is directed to the input file (if a -file of the same name as the backup file already exists, it will be replaced -silently). This makes it possible to write a filter that rewrites its input -file in place. If the *backup* parameter is given (typically as +**Optional in-place filtering:** if the keyword argument ``inplace=True`` is +passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the +file is moved to a backup file and standard output is directed to the input file +(if a file of the same name as the backup file already exists, it will be +replaced silently). This makes it possible to write a filter that rewrites its +input file in place. If the *backup* parameter is given (typically as ``backup='.<some extension>'``), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is ``'.bak'`` and it is deleted when the output file is closed. In-place filtering is disabled |