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