@@ -24,7 +24,7 @@ as the first argument to :func:`.input`. A single file name is also allowed.
2424
2525All files are opened in text mode by default, but you can override this by
2626specifying the *mode * parameter in the call to :func: `.input ` or
27- :class: `FileInput() `. If an I/O error occurs during opening or reading a file,
27+ :class: `FileInput `. If an I/O error occurs during opening or reading a file,
2828:exc: `IOError ` is raised.
2929
3030If ``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:
5454 during iteration. The parameters to this function will be passed along to the
5555 constructor of the :class: `FileInput ` class.
5656
57+ The :class: `FileInput ` instance can be used as a context manager in the
58+ :keyword: `with ` statement. In this example, *input * is closed after the
59+ :keyword: `with ` statement is exited, even if an exception occurs::
60+
61+ with fileinput.input(files=('spam.txt', 'eggs.txt')) as input:
62+ process(input)
63+
64+ .. versionchanged :: 3.2
65+ Can be used as a context manager.
66+
5767
5868The following functions use the global state created by :func: `fileinput.input `;
5969if there is no active state, :exc: `RuntimeError ` is raised.
@@ -132,13 +142,23 @@ available for subclassing as well:
132142 *filename * and *mode *, and returns an accordingly opened file-like object. You
133143 cannot use *inplace * and *openhook * together.
134144
145+ A :class: `FileInput ` instance can be used as a context manager in the
146+ :keyword: `with ` statement. In this example, *input * is closed after the
147+ :keyword: `with ` statement is exited, even if an exception occurs::
148+
149+ with FileInput(files=('spam.txt', 'eggs.txt')) as input:
150+ process(input)
151+
152+ .. versionchanged :: 3.2
153+ Can be used as a context manager.
154+
135155
136- **Optional in-place filtering: ** if the keyword argument ``inplace=1 `` is passed
137- to :func: `fileinput.input ` or to the :class: `FileInput ` constructor, the file is
138- moved to a backup file and standard output is directed to the input file (if a
139- file of the same name as the backup file already exists, it will be replaced
140- silently). This makes it possible to write a filter that rewrites its input
141- file in place. If the *backup * parameter is given (typically as
156+ **Optional in-place filtering: ** if the keyword argument ``inplace=True `` is
157+ passed to :func: `fileinput.input ` or to the :class: `FileInput ` constructor, the
158+ file is moved to a backup file and standard output is directed to the input file
159+ (if a file of the same name as the backup file already exists, it will be
160+ replaced silently). This makes it possible to write a filter that rewrites its
161+ input file in place. If the *backup * parameter is given (typically as
142162``backup='.<some extension>' ``), it specifies the extension for the backup file,
143163and the backup file remains around; by default, the extension is ``'.bak' `` and
144164it is deleted when the output file is closed. In-place filtering is disabled
0 commit comments