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