1:mod:`fileinput` --- Iterate over lines from multiple input streams 2=================================================================== 3 4.. module:: fileinput 5 :synopsis: Loop over standard input or a list of files. 6 7.. moduleauthor:: Guido van Rossum <guido@python.org> 8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> 9 10**Source code:** :source:`Lib/fileinput.py` 11 12-------------- 13 14This module implements a helper class and functions to quickly write a 15loop over standard input or a list of files. If you just want to read or 16write one file see :func:`open`. 17 18The typical use is:: 19 20 import fileinput 21 for line in fileinput.input(): 22 process(line) 23 24This iterates over the lines of all files listed in ``sys.argv[1:]``, defaulting 25to ``sys.stdin`` if the list is empty. If a filename is ``'-'``, it is also 26replaced by ``sys.stdin``. To specify an alternative list of filenames, pass it 27as the first argument to :func:`.input`. A single file name is also allowed. 28 29All files are opened in text mode by default, but you can override this by 30specifying the *mode* parameter in the call to :func:`.input` or 31:class:`FileInput`. If an I/O error occurs during opening or reading a file, 32:exc:`OSError` is raised. 33 34.. versionchanged:: 3.3 35 :exc:`IOError` used to be raised; it is now an alias of :exc:`OSError`. 36 37If ``sys.stdin`` is used more than once, the second and further use will return 38no lines, except perhaps for interactive use, or if it has been explicitly reset 39(e.g. using ``sys.stdin.seek(0)``). 40 41Empty files are opened and immediately closed; the only time their presence in 42the list of filenames is noticeable at all is when the last file opened is 43empty. 44 45Lines are returned with any newlines intact, which means that the last line in 46a file may not have one. 47 48You can control how files are opened by providing an opening hook via the 49*openhook* parameter to :func:`fileinput.input` or :class:`FileInput()`. The 50hook must be a function that takes two arguments, *filename* and *mode*, and 51returns an accordingly opened file-like object. Two useful hooks are already 52provided by this module. 53 54The following function is the primary interface of this module: 55 56 57.. function:: input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None) 58 59 Create an instance of the :class:`FileInput` class. The instance will be used 60 as global state for the functions of this module, and is also returned to use 61 during iteration. The parameters to this function will be passed along to the 62 constructor of the :class:`FileInput` class. 63 64 The :class:`FileInput` instance can be used as a context manager in the 65 :keyword:`with` statement. In this example, *input* is closed after the 66 :keyword:`with` statement is exited, even if an exception occurs:: 67 68 with fileinput.input(files=('spam.txt', 'eggs.txt')) as f: 69 for line in f: 70 process(line) 71 72 .. versionchanged:: 3.2 73 Can be used as a context manager. 74 75 .. deprecated-removed:: 3.6 3.8 76 The *bufsize* parameter. 77 78The following functions use the global state created by :func:`fileinput.input`; 79if there is no active state, :exc:`RuntimeError` is raised. 80 81 82.. function:: filename() 83 84 Return the name of the file currently being read. Before the first line has 85 been read, returns ``None``. 86 87 88.. function:: fileno() 89 90 Return the integer "file descriptor" for the current file. When no file is 91 opened (before the first line and between files), returns ``-1``. 92 93 94.. function:: lineno() 95 96 Return the cumulative line number of the line that has just been read. Before 97 the first line has been read, returns ``0``. After the last line of the last 98 file has been read, returns the line number of that line. 99 100 101.. function:: filelineno() 102 103 Return the line number in the current file. Before the first line has been 104 read, returns ``0``. After the last line of the last file has been read, 105 returns the line number of that line within the file. 106 107 108.. function:: isfirstline() 109 110 Returns true if the line just read is the first line of its file, otherwise 111 returns false. 112 113 114.. function:: isstdin() 115 116 Returns true if the last line was read from ``sys.stdin``, otherwise returns 117 false. 118 119 120.. function:: nextfile() 121 122 Close the current file so that the next iteration will read the first line from 123 the next file (if any); lines not read from the file will not count towards the 124 cumulative line count. The filename is not changed until after the first line 125 of the next file has been read. Before the first line has been read, this 126 function has no effect; it cannot be used to skip the first file. After the 127 last line of the last file has been read, this function has no effect. 128 129 130.. function:: close() 131 132 Close the sequence. 133 134The class which implements the sequence behavior provided by the module is 135available for subclassing as well: 136 137 138.. class:: FileInput(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None) 139 140 Class :class:`FileInput` is the implementation; its methods :meth:`filename`, 141 :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`, 142 :meth:`isstdin`, :meth:`nextfile` and :meth:`close` correspond to the 143 functions of the same name in the module. In addition it has a 144 :meth:`~io.TextIOBase.readline` method which returns the next input line, 145 and a :meth:`__getitem__` method which implements the sequence behavior. 146 The sequence must be accessed in strictly sequential order; random access 147 and :meth:`~io.TextIOBase.readline` cannot be mixed. 148 149 With *mode* you can specify which file mode will be passed to :func:`open`. It 150 must be one of ``'r'``, ``'rU'``, ``'U'`` and ``'rb'``. 151 152 The *openhook*, when given, must be a function that takes two arguments, 153 *filename* and *mode*, and returns an accordingly opened file-like object. You 154 cannot use *inplace* and *openhook* together. 155 156 A :class:`FileInput` instance can be used as a context manager in the 157 :keyword:`with` statement. In this example, *input* is closed after the 158 :keyword:`with` statement is exited, even if an exception occurs:: 159 160 with FileInput(files=('spam.txt', 'eggs.txt')) as input: 161 process(input) 162 163 .. versionchanged:: 3.2 164 Can be used as a context manager. 165 166 .. deprecated:: 3.4 167 The ``'rU'`` and ``'U'`` modes. 168 169 .. deprecated-removed:: 3.6 3.8 170 The *bufsize* parameter. 171 172 173**Optional in-place filtering:** if the keyword argument ``inplace=True`` is 174passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the 175file is moved to a backup file and standard output is directed to the input file 176(if a file of the same name as the backup file already exists, it will be 177replaced silently). This makes it possible to write a filter that rewrites its 178input file in place. If the *backup* parameter is given (typically as 179``backup='.<some extension>'``), it specifies the extension for the backup file, 180and the backup file remains around; by default, the extension is ``'.bak'`` and 181it is deleted when the output file is closed. In-place filtering is disabled 182when standard input is read. 183 184 185The two following opening hooks are provided by this module: 186 187.. function:: hook_compressed(filename, mode) 188 189 Transparently opens files compressed with gzip and bzip2 (recognized by the 190 extensions ``'.gz'`` and ``'.bz2'``) using the :mod:`gzip` and :mod:`bz2` 191 modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is 192 opened normally (ie, using :func:`open` without any decompression). 193 194 Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)`` 195 196 197.. function:: hook_encoded(encoding, errors=None) 198 199 Returns a hook which opens each file with :func:`open`, using the given 200 *encoding* and *errors* to read the file. 201 202 Usage example: ``fi = 203 fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8", 204 "surrogateescape"))`` 205 206 .. versionchanged:: 3.6 207 Added the optional *errors* parameter. 208