diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Ffileinput.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Ffileinput.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Ffileinput.html | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Ffileinput.html b/devdocs/python~3.12/library%2Ffileinput.html new file mode 100644 index 00000000..d65be7c0 --- /dev/null +++ b/devdocs/python~3.12/library%2Ffileinput.html @@ -0,0 +1,52 @@ + <span id="fileinput-iterate-over-lines-from-multiple-input-streams"></span><h1>fileinput — Iterate over lines from multiple input streams</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/fileinput.py">Lib/fileinput.py</a></p> <p>This module implements a helper class and functions to quickly write a loop over standard input or a list of files. If you just want to read or write one file see <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>.</p> <p>The typical use is:</p> <pre data-language="python">import fileinput +for line in fileinput.input(encoding="utf-8"): + process(line) +</pre> <p>This iterates over the lines of all files listed in <code>sys.argv[1:]</code>, defaulting to <code>sys.stdin</code> if the list is empty. If a filename is <code>'-'</code>, it is also replaced by <code>sys.stdin</code> and the optional arguments <em>mode</em> and <em>openhook</em> are ignored. To specify an alternative list of filenames, pass it as the first argument to <a class="reference internal" href="#fileinput.input" title="fileinput.input"><code>input()</code></a>. A single file name is also allowed.</p> <p>All files are opened in text mode by default, but you can override this by specifying the <em>mode</em> parameter in the call to <a class="reference internal" href="#fileinput.input" title="fileinput.input"><code>input()</code></a> or <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a>. If an I/O error occurs during opening or reading a file, <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a> is raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions#IOError" title="IOError"><code>IOError</code></a> used to be raised; it is now an alias of <a class="reference internal" href="exceptions#OSError" title="OSError"><code>OSError</code></a>.</p> </div> <p>If <code>sys.stdin</code> is used more than once, the second and further use will return no lines, except perhaps for interactive use, or if it has been explicitly reset (e.g. using <code>sys.stdin.seek(0)</code>).</p> <p>Empty files are opened and immediately closed; the only time their presence in the list of filenames is noticeable at all is when the last file opened is empty.</p> <p>Lines are returned with any newlines intact, which means that the last line in a file may not have one.</p> <p>You can control how files are opened by providing an opening hook via the <em>openhook</em> parameter to <a class="reference internal" href="#fileinput.input" title="fileinput.input"><code>fileinput.input()</code></a> or <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput()</code></a>. The hook must be a function that takes two arguments, <em>filename</em> and <em>mode</em>, and returns an accordingly opened file-like object. If <em>encoding</em> and/or <em>errors</em> are specified, they will be passed to the hook as additional keyword arguments. This module provides a <a class="reference internal" href="#fileinput.hook_compressed" title="fileinput.hook_compressed"><code>hook_compressed()</code></a> to support compressed files.</p> <p>The following function is the primary interface of this module:</p> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.input"> +<code>fileinput.input(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)</code> </dt> <dd> +<p>Create an instance of the <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> class. The instance will be used as global state for the functions of this module, and is also returned to use during iteration. The parameters to this function will be passed along to the constructor of the <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> class.</p> <p>The <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> instance can be used as a context manager in the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement. In this example, <em>input</em> is closed after the <code>with</code> statement is exited, even if an exception occurs:</p> <pre data-language="python">with fileinput.input(files=('spam.txt', 'eggs.txt'), encoding="utf-8") as f: + for line in f: + process(line) +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Can be used as a context manager.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The keyword parameters <em>mode</em> and <em>openhook</em> are now keyword-only.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The keyword-only parameter <em>encoding</em> and <em>errors</em> are added.</p> </div> </dd> +</dl> <p>The following functions use the global state created by <a class="reference internal" href="#fileinput.input" title="fileinput.input"><code>fileinput.input()</code></a>; if there is no active state, <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.filename"> +<code>fileinput.filename()</code> </dt> <dd> +<p>Return the name of the file currently being read. Before the first line has been read, returns <code>None</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.fileno"> +<code>fileinput.fileno()</code> </dt> <dd> +<p>Return the integer “file descriptor” for the current file. When no file is opened (before the first line and between files), returns <code>-1</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.lineno"> +<code>fileinput.lineno()</code> </dt> <dd> +<p>Return the cumulative line number of the line that has just been read. Before the first line has been read, returns <code>0</code>. After the last line of the last file has been read, returns the line number of that line.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.filelineno"> +<code>fileinput.filelineno()</code> </dt> <dd> +<p>Return the line number in the current file. Before the first line has been read, returns <code>0</code>. After the last line of the last file has been read, returns the line number of that line within the file.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.isfirstline"> +<code>fileinput.isfirstline()</code> </dt> <dd> +<p>Return <code>True</code> if the line just read is the first line of its file, otherwise return <code>False</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.isstdin"> +<code>fileinput.isstdin()</code> </dt> <dd> +<p>Return <code>True</code> if the last line was read from <code>sys.stdin</code>, otherwise return <code>False</code>.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.nextfile"> +<code>fileinput.nextfile()</code> </dt> <dd> +<p>Close the current file so that the next iteration will read the first line from the next file (if any); lines not read from the file will not count towards the cumulative line count. The filename is not changed until after the first line of the next file has been read. Before the first line has been read, this function has no effect; it cannot be used to skip the first file. After the last line of the last file has been read, this function has no effect.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.close"> +<code>fileinput.close()</code> </dt> <dd> +<p>Close the sequence.</p> </dd> +</dl> <p>The class which implements the sequence behavior provided by the module is available for subclassing as well:</p> <dl class="py class"> <dt class="sig sig-object py" id="fileinput.FileInput"> +<code>class fileinput.FileInput(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)</code> </dt> <dd> +<p>Class <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> is the implementation; its methods <a class="reference internal" href="#fileinput.filename" title="fileinput.filename"><code>filename()</code></a>, <a class="reference internal" href="#fileinput.fileno" title="fileinput.fileno"><code>fileno()</code></a>, <a class="reference internal" href="#fileinput.lineno" title="fileinput.lineno"><code>lineno()</code></a>, <a class="reference internal" href="#fileinput.filelineno" title="fileinput.filelineno"><code>filelineno()</code></a>, <a class="reference internal" href="#fileinput.isfirstline" title="fileinput.isfirstline"><code>isfirstline()</code></a>, <a class="reference internal" href="#fileinput.isstdin" title="fileinput.isstdin"><code>isstdin()</code></a>, <a class="reference internal" href="#fileinput.nextfile" title="fileinput.nextfile"><code>nextfile()</code></a> and <a class="reference internal" href="#fileinput.close" title="fileinput.close"><code>close()</code></a> correspond to the functions of the same name in the module. In addition it is <a class="reference internal" href="../glossary#term-iterable"><span class="xref std std-term">iterable</span></a> and has a <a class="reference internal" href="io#io.TextIOBase.readline" title="io.TextIOBase.readline"><code>readline()</code></a> method which returns the next input line. The sequence must be accessed in strictly sequential order; random access and <a class="reference internal" href="io#io.TextIOBase.readline" title="io.TextIOBase.readline"><code>readline()</code></a> cannot be mixed.</p> <p>With <em>mode</em> you can specify which file mode will be passed to <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>. It must be one of <code>'r'</code> and <code>'rb'</code>.</p> <p>The <em>openhook</em>, when given, must be a function that takes two arguments, <em>filename</em> and <em>mode</em>, and returns an accordingly opened file-like object. You cannot use <em>inplace</em> and <em>openhook</em> together.</p> <p>You can specify <em>encoding</em> and <em>errors</em> that is passed to <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> or <em>openhook</em>.</p> <p>A <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> instance can be used as a context manager in the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement. In this example, <em>input</em> is closed after the <code>with</code> statement is exited, even if an exception occurs:</p> <pre data-language="python">with FileInput(files=('spam.txt', 'eggs.txt')) as input: + process(input) +</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.2: </span>Can be used as a context manager.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>The keyword parameter <em>mode</em> and <em>openhook</em> are now keyword-only.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The keyword-only parameter <em>encoding</em> and <em>errors</em> are added.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.11: </span>The <code>'rU'</code> and <code>'U'</code> modes and the <code>__getitem__()</code> method have been removed.</p> </div> </dd> +</dl> <p><strong>Optional in-place filtering:</strong> if the keyword argument <code>inplace=True</code> is passed to <a class="reference internal" href="#fileinput.input" title="fileinput.input"><code>fileinput.input()</code></a> or to the <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> 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 <em>backup</em> parameter is given (typically as <code>backup='.<some extension>'</code>), it specifies the extension for the backup file, and the backup file remains around; by default, the extension is <code>'.bak'</code> and it is deleted when the output file is closed. In-place filtering is disabled when standard input is read.</p> <p>The two following opening hooks are provided by this module:</p> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.hook_compressed"> +<code>fileinput.hook_compressed(filename, mode, *, encoding=None, errors=None)</code> </dt> <dd> +<p>Transparently opens files compressed with gzip and bzip2 (recognized by the extensions <code>'.gz'</code> and <code>'.bz2'</code>) using the <a class="reference internal" href="gzip#module-gzip" title="gzip: Interfaces for gzip compression and decompression using file objects."><code>gzip</code></a> and <a class="reference internal" href="bz2#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code>bz2</code></a> modules. If the filename extension is not <code>'.gz'</code> or <code>'.bz2'</code>, the file is opened normally (ie, using <a class="reference internal" href="functions#open" title="open"><code>open()</code></a> without any decompression).</p> <p>The <em>encoding</em> and <em>errors</em> values are passed to <a class="reference internal" href="io#io.TextIOWrapper" title="io.TextIOWrapper"><code>io.TextIOWrapper</code></a> for compressed files and open for normal files.</p> <p>Usage example: <code>fi = fileinput.FileInput(openhook=fileinput.hook_compressed, encoding="utf-8")</code></p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>The keyword-only parameter <em>encoding</em> and <em>errors</em> are added.</p> </div> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="fileinput.hook_encoded"> +<code>fileinput.hook_encoded(encoding, errors=None)</code> </dt> <dd> +<p>Returns a hook which opens each file with <a class="reference internal" href="functions#open" title="open"><code>open()</code></a>, using the given <em>encoding</em> and <em>errors</em> to read the file.</p> <p>Usage example: <code>fi = +fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8", +"surrogateescape"))</code></p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>Added the optional <em>errors</em> parameter.</p> </div> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.10: </span>This function is deprecated since <a class="reference internal" href="#fileinput.input" title="fileinput.input"><code>fileinput.input()</code></a> and <a class="reference internal" href="#fileinput.FileInput" title="fileinput.FileInput"><code>FileInput</code></a> now have <em>encoding</em> and <em>errors</em> parameters.</p> </div> </dd> +</dl> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/fileinput.html" class="_attribution-link">https://docs.python.org/3.12/library/fileinput.html</a> + </p> +</div> |
