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