Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Guido van Rossum <guido@python.org> |
| 8 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 9 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/fileinput.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 12 | -------------- |
| 13 | |
Thomas Wouters | 1b7f891 | 2007-09-19 03:06:30 +0000 | [diff] [blame] | 14 | This module implements a helper class and functions to quickly write a |
| 15 | loop over standard input or a list of files. If you just want to read or |
| 16 | write one file see :func:`open`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | |
| 18 | The typical use is:: |
| 19 | |
| 20 | import fileinput |
| 21 | for line in fileinput.input(): |
| 22 | process(line) |
| 23 | |
| 24 | This iterates over the lines of all files listed in ``sys.argv[1:]``, defaulting |
| 25 | to ``sys.stdin`` if the list is empty. If a filename is ``'-'``, it is also |
Michele Angrisano | aca273e | 2019-06-02 23:01:49 +0200 | [diff] [blame] | 26 | replaced by ``sys.stdin`` and the optional arguments *mode* and *openhook* |
| 27 | are ignored. To specify an alternative list of filenames, pass it as the |
| 28 | first argument to :func:`.input`. A single file name is also allowed. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 29 | |
| 30 | All files are opened in text mode by default, but you can override this by |
Georg Brandl | 96593ed | 2007-09-07 14:15:41 +0000 | [diff] [blame] | 31 | specifying the *mode* parameter in the call to :func:`.input` or |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 32 | :class:`FileInput`. If an I/O error occurs during opening or reading a file, |
Antoine Pitrou | 4272d6a | 2011-10-12 19:10:10 +0200 | [diff] [blame] | 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`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
| 38 | If ``sys.stdin`` is used more than once, the second and further use will return |
| 39 | no lines, except perhaps for interactive use, or if it has been explicitly reset |
| 40 | (e.g. using ``sys.stdin.seek(0)``). |
| 41 | |
| 42 | Empty files are opened and immediately closed; the only time their presence in |
| 43 | the list of filenames is noticeable at all is when the last file opened is |
| 44 | empty. |
| 45 | |
| 46 | Lines are returned with any newlines intact, which means that the last line in |
| 47 | a file may not have one. |
| 48 | |
| 49 | You can control how files are opened by providing an opening hook via the |
| 50 | *openhook* parameter to :func:`fileinput.input` or :class:`FileInput()`. The |
| 51 | hook must be a function that takes two arguments, *filename* and *mode*, and |
| 52 | returns an accordingly opened file-like object. Two useful hooks are already |
| 53 | provided by this module. |
| 54 | |
| 55 | The following function is the primary interface of this module: |
| 56 | |
| 57 | |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 58 | .. function:: input(files=None, inplace=False, backup='', *, mode='r', openhook=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 67 | :keyword:`!with` statement is exited, even if an exception occurs:: |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 68 | |
Raymond Hettinger | 7fefaff | 2010-09-05 23:50:32 +0000 | [diff] [blame] | 69 | with fileinput.input(files=('spam.txt', 'eggs.txt')) as f: |
| 70 | for line in f: |
| 71 | process(line) |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 72 | |
| 73 | .. versionchanged:: 3.2 |
| 74 | Can be used as a context manager. |
| 75 | |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 76 | .. versionchanged:: 3.8 |
| 77 | The keyword parameters *mode* and *openhook* are now keyword-only. |
| 78 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 79 | |
| 80 | The following functions use the global state created by :func:`fileinput.input`; |
| 81 | if 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 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | 138ccbb | 2019-11-12 16:57:03 +0200 | [diff] [blame] | 112 | Return ``True`` if the line just read is the first line of its file, otherwise |
| 113 | return ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | .. function:: isstdin() |
| 117 | |
Serhiy Storchaka | 138ccbb | 2019-11-12 16:57:03 +0200 | [diff] [blame] | 118 | Return ``True`` if the last line was read from ``sys.stdin``, otherwise return |
| 119 | ``False``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
| 136 | The class which implements the sequence behavior provided by the module is |
| 137 | available for subclassing as well: |
| 138 | |
| 139 | |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 140 | .. class:: FileInput(files=None, inplace=False, backup='', *, mode='r', openhook=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 141 | |
| 142 | Class :class:`FileInput` is the implementation; its methods :meth:`filename`, |
| 143 | :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`, |
Serhiy Storchaka | bfdcd43 | 2013-10-13 23:09:14 +0300 | [diff] [blame] | 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. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | |
Victor Stinner | e471e72 | 2019-10-28 15:40:08 +0100 | [diff] [blame] | 151 | With *mode* you can specify which file mode will be passed to :func:`open`. |
| 152 | It must be ``'r'`` or ``'rb'``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | 2b57c43 | 2018-12-19 08:09:46 +0200 | [diff] [blame] | 160 | :keyword:`!with` statement is exited, even if an exception occurs:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 161 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 162 | with FileInput(files=('spam.txt', 'eggs.txt')) as input: |
| 163 | process(input) |
| 164 | |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 165 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 166 | .. versionchanged:: 3.2 |
| 167 | Can be used as a context manager. |
| 168 | |
Berker Peksag | 84a13fb | 2018-08-11 09:05:04 +0300 | [diff] [blame] | 169 | .. deprecated:: 3.8 |
| 170 | Support for :meth:`__getitem__` method is deprecated. |
| 171 | |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 172 | .. versionchanged:: 3.8 |
| 173 | The keyword parameter *mode* and *openhook* are now keyword-only. |
| 174 | |
Victor Stinner | e471e72 | 2019-10-28 15:40:08 +0100 | [diff] [blame] | 175 | .. versionchanged:: 3.9 |
| 176 | The ``'rU'`` and ``'U'`` modes have been removed. |
Matthias Bussonnier | 1a3faf9 | 2019-05-20 13:44:11 -0700 | [diff] [blame] | 177 | |
Georg Brandl | 6cb7b65 | 2010-07-31 20:08:15 +0000 | [diff] [blame] | 178 | |
| 179 | **Optional in-place filtering:** if the keyword argument ``inplace=True`` is |
| 180 | passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the |
| 181 | file is moved to a backup file and standard output is directed to the input file |
| 182 | (if a file of the same name as the backup file already exists, it will be |
| 183 | replaced silently). This makes it possible to write a filter that rewrites its |
| 184 | input file in place. If the *backup* parameter is given (typically as |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 185 | ``backup='.<some extension>'``), it specifies the extension for the backup file, |
| 186 | and the backup file remains around; by default, the extension is ``'.bak'`` and |
| 187 | it is deleted when the output file is closed. In-place filtering is disabled |
| 188 | when standard input is read. |
| 189 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 190 | |
| 191 | The two following opening hooks are provided by this module: |
| 192 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | .. function:: hook_compressed(filename, mode) |
| 194 | |
| 195 | Transparently opens files compressed with gzip and bzip2 (recognized by the |
| 196 | extensions ``'.gz'`` and ``'.bz2'``) using the :mod:`gzip` and :mod:`bz2` |
| 197 | modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is |
| 198 | opened normally (ie, using :func:`open` without any decompression). |
| 199 | |
| 200 | Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)`` |
| 201 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 202 | |
Serhiy Storchaka | b275210 | 2016-04-27 23:13:46 +0300 | [diff] [blame] | 203 | .. function:: hook_encoded(encoding, errors=None) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 204 | |
Serhiy Storchaka | a87e6ba | 2016-04-27 23:06:15 +0300 | [diff] [blame] | 205 | Returns a hook which opens each file with :func:`open`, using the given |
Serhiy Storchaka | b275210 | 2016-04-27 23:13:46 +0300 | [diff] [blame] | 206 | *encoding* and *errors* to read the file. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 207 | |
| 208 | Usage example: ``fi = |
Serhiy Storchaka | b275210 | 2016-04-27 23:13:46 +0300 | [diff] [blame] | 209 | fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8", |
| 210 | "surrogateescape"))`` |
| 211 | |
| 212 | .. versionchanged:: 3.6 |
| 213 | Added the optional *errors* parameter. |