blob: 819640045cadf737212abcb4f8cf40eb1676493c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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 Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Guido van Rossum <guido@python.org>
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/fileinput.py`
Georg Brandl116aa622007-08-15 14:28:22 +000011
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Thomas Wouters1b7f8912007-09-19 03:06:30 +000014This 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`.
Georg Brandl116aa622007-08-15 14:28:22 +000017
18The typical use is::
19
20 import fileinput
Inada Naoki333d10c2021-04-14 14:12:58 +090021 for line in fileinput.input(encoding="utf-8"):
Georg Brandl116aa622007-08-15 14:28:22 +000022 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
Michele Angrisanoaca273e2019-06-02 23:01:49 +020026replaced 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.
Georg Brandl116aa622007-08-15 14:28:22 +000029
30All files are opened in text mode by default, but you can override this by
Georg Brandl96593ed2007-09-07 14:15:41 +000031specifying the *mode* parameter in the call to :func:`.input` or
Georg Brandl6cb7b652010-07-31 20:08:15 +000032:class:`FileInput`. If an I/O error occurs during opening or reading a file,
Antoine Pitrou4272d6a2011-10-12 19:10:10 +020033: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 Brandl116aa622007-08-15 14:28:22 +000037
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
Inada Naoki333d10c2021-04-14 14:12:58 +090052returns an accordingly opened file-like object. If *encoding* and/or *errors*
53are specified, they will be passed to the hook as aditional keyword arguments.
54This module provides a :func:`hook_encoded` to support compressed files.
Georg Brandl116aa622007-08-15 14:28:22 +000055
56The following function is the primary interface of this module:
57
58
Inada Naoki333d10c2021-04-14 14:12:58 +090059.. function:: input(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +000060
61 Create an instance of the :class:`FileInput` class. The instance will be used
62 as global state for the functions of this module, and is also returned to use
63 during iteration. The parameters to this function will be passed along to the
64 constructor of the :class:`FileInput` class.
65
Georg Brandl6cb7b652010-07-31 20:08:15 +000066 The :class:`FileInput` instance can be used as a context manager in the
67 :keyword:`with` statement. In this example, *input* is closed after the
Serhiy Storchaka2b57c432018-12-19 08:09:46 +020068 :keyword:`!with` statement is exited, even if an exception occurs::
Georg Brandl6cb7b652010-07-31 20:08:15 +000069
Inada Naoki333d10c2021-04-14 14:12:58 +090070 with fileinput.input(files=('spam.txt', 'eggs.txt'), encoding="utf-8") as f:
Raymond Hettinger7fefaff2010-09-05 23:50:32 +000071 for line in f:
72 process(line)
Georg Brandl6cb7b652010-07-31 20:08:15 +000073
74 .. versionchanged:: 3.2
75 Can be used as a context manager.
76
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -070077 .. versionchanged:: 3.8
78 The keyword parameters *mode* and *openhook* are now keyword-only.
79
Inada Naoki333d10c2021-04-14 14:12:58 +090080 .. versionchanged:: 3.10
81 The keyword-only parameter *encoding* and *errors* are added.
82
Georg Brandl116aa622007-08-15 14:28:22 +000083
84The following functions use the global state created by :func:`fileinput.input`;
85if there is no active state, :exc:`RuntimeError` is raised.
86
87
88.. function:: filename()
89
90 Return the name of the file currently being read. Before the first line has
91 been read, returns ``None``.
92
93
94.. function:: fileno()
95
96 Return the integer "file descriptor" for the current file. When no file is
97 opened (before the first line and between files), returns ``-1``.
98
Georg Brandl116aa622007-08-15 14:28:22 +000099
100.. function:: lineno()
101
102 Return the cumulative line number of the line that has just been read. Before
103 the first line has been read, returns ``0``. After the last line of the last
104 file has been read, returns the line number of that line.
105
106
107.. function:: filelineno()
108
109 Return the line number in the current file. Before the first line has been
110 read, returns ``0``. After the last line of the last file has been read,
111 returns the line number of that line within the file.
112
113
114.. function:: isfirstline()
115
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +0200116 Return ``True`` if the line just read is the first line of its file, otherwise
117 return ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119
120.. function:: isstdin()
121
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +0200122 Return ``True`` if the last line was read from ``sys.stdin``, otherwise return
123 ``False``.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
126.. function:: nextfile()
127
128 Close the current file so that the next iteration will read the first line from
129 the next file (if any); lines not read from the file will not count towards the
130 cumulative line count. The filename is not changed until after the first line
131 of the next file has been read. Before the first line has been read, this
132 function has no effect; it cannot be used to skip the first file. After the
133 last line of the last file has been read, this function has no effect.
134
135
136.. function:: close()
137
138 Close the sequence.
139
140The class which implements the sequence behavior provided by the module is
141available for subclassing as well:
142
143
Inada Naoki333d10c2021-04-14 14:12:58 +0900144.. class:: FileInput(files=None, inplace=False, backup='', *, mode='r', openhook=None, encoding=None, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146 Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
147 :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300148 :meth:`isstdin`, :meth:`nextfile` and :meth:`close` correspond to the
149 functions of the same name in the module. In addition it has a
150 :meth:`~io.TextIOBase.readline` method which returns the next input line,
151 and a :meth:`__getitem__` method which implements the sequence behavior.
152 The sequence must be accessed in strictly sequential order; random access
153 and :meth:`~io.TextIOBase.readline` cannot be mixed.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
Victor Stinner942f7a22020-03-04 18:50:22 +0100155 With *mode* you can specify which file mode will be passed to :func:`open`. It
156 must be one of ``'r'``, ``'rU'``, ``'U'`` and ``'rb'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158 The *openhook*, when given, must be a function that takes two arguments,
159 *filename* and *mode*, and returns an accordingly opened file-like object. You
160 cannot use *inplace* and *openhook* together.
161
Inada Naoki333d10c2021-04-14 14:12:58 +0900162 You can specify *encoding* and *errors* that is passed to :func:`open` or *openhook*.
163
Georg Brandl6cb7b652010-07-31 20:08:15 +0000164 A :class:`FileInput` instance can be used as a context manager in the
165 :keyword:`with` statement. In this example, *input* is closed after the
Serhiy Storchaka2b57c432018-12-19 08:09:46 +0200166 :keyword:`!with` statement is exited, even if an exception occurs::
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Georg Brandl6cb7b652010-07-31 20:08:15 +0000168 with FileInput(files=('spam.txt', 'eggs.txt')) as input:
169 process(input)
170
171 .. versionchanged:: 3.2
172 Can be used as a context manager.
173
Victor Stinner942f7a22020-03-04 18:50:22 +0100174 .. deprecated:: 3.4
175 The ``'rU'`` and ``'U'`` modes.
176
Berker Peksag84a13fb2018-08-11 09:05:04 +0300177 .. deprecated:: 3.8
178 Support for :meth:`__getitem__` method is deprecated.
179
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700180 .. versionchanged:: 3.8
181 The keyword parameter *mode* and *openhook* are now keyword-only.
182
Inada Naoki333d10c2021-04-14 14:12:58 +0900183 .. versionchanged:: 3.10
184 The keyword-only parameter *encoding* and *errors* are added.
Matthias Bussonnier1a3faf92019-05-20 13:44:11 -0700185
Georg Brandl6cb7b652010-07-31 20:08:15 +0000186
187**Optional in-place filtering:** if the keyword argument ``inplace=True`` is
188passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the
189file is moved to a backup file and standard output is directed to the input file
190(if a file of the same name as the backup file already exists, it will be
191replaced silently). This makes it possible to write a filter that rewrites its
192input file in place. If the *backup* parameter is given (typically as
Georg Brandl116aa622007-08-15 14:28:22 +0000193``backup='.<some extension>'``), it specifies the extension for the backup file,
194and the backup file remains around; by default, the extension is ``'.bak'`` and
195it is deleted when the output file is closed. In-place filtering is disabled
196when standard input is read.
197
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199The two following opening hooks are provided by this module:
200
Inada Naoki333d10c2021-04-14 14:12:58 +0900201.. function:: hook_compressed(filename, mode, *, encoding=None, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203 Transparently opens files compressed with gzip and bzip2 (recognized by the
204 extensions ``'.gz'`` and ``'.bz2'``) using the :mod:`gzip` and :mod:`bz2`
205 modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is
206 opened normally (ie, using :func:`open` without any decompression).
207
Inada Naoki333d10c2021-04-14 14:12:58 +0900208 The *encoding* and *errors* values are passed to to :class:`io.TextIOWrapper`
209 for compressed files and open for normal files.
210
211 Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed, encoding="utf-8")``
212
213 .. versionchanged:: 3.10
214 The keyword-only parameter *encoding* and *errors* are added.
Georg Brandl116aa622007-08-15 14:28:22 +0000215
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Serhiy Storchakab2752102016-04-27 23:13:46 +0300217.. function:: hook_encoded(encoding, errors=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000218
Serhiy Storchakaa87e6ba2016-04-27 23:06:15 +0300219 Returns a hook which opens each file with :func:`open`, using the given
Serhiy Storchakab2752102016-04-27 23:13:46 +0300220 *encoding* and *errors* to read the file.
Georg Brandl116aa622007-08-15 14:28:22 +0000221
222 Usage example: ``fi =
Serhiy Storchakab2752102016-04-27 23:13:46 +0300223 fileinput.FileInput(openhook=fileinput.hook_encoded("utf-8",
224 "surrogateescape"))``
225
226 .. versionchanged:: 3.6
227 Added the optional *errors* parameter.
Inada Naoki333d10c2021-04-14 14:12:58 +0900228
229 .. deprecated:: 3.10
230 This function is deprecated since :func:`input` and :class:`FileInput`
231 now have *encoding* and *errors* parameters.