blob: aa4c529b2a6797b6339f2ca19c88c4dff9a1b191 [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
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``. To specify an alternative list of filenames, pass it
Georg Brandl96593ed2007-09-07 14:15:41 +000027as the first argument to :func:`.input`. A single file name is also allowed.
Georg Brandl116aa622007-08-15 14:28:22 +000028
29All files are opened in text mode by default, but you can override this by
Georg Brandl96593ed2007-09-07 14:15:41 +000030specifying the *mode* parameter in the call to :func:`.input` or
Georg Brandl6cb7b652010-07-31 20:08:15 +000031:class:`FileInput`. If an I/O error occurs during opening or reading a file,
Antoine Pitrou4272d6a2011-10-12 19:10:10 +020032:exc:`OSError` is raised.
33
34.. versionchanged:: 3.3
35 :exc:`IOError` used to be raised; it is now an alias of :exc:`OSError`.
Georg Brandl116aa622007-08-15 14:28:22 +000036
37If ``sys.stdin`` is used more than once, the second and further use will return
38no lines, except perhaps for interactive use, or if it has been explicitly reset
39(e.g. using ``sys.stdin.seek(0)``).
40
41Empty files are opened and immediately closed; the only time their presence in
42the list of filenames is noticeable at all is when the last file opened is
43empty.
44
45Lines are returned with any newlines intact, which means that the last line in
46a file may not have one.
47
48You can control how files are opened by providing an opening hook via the
49*openhook* parameter to :func:`fileinput.input` or :class:`FileInput()`. The
50hook must be a function that takes two arguments, *filename* and *mode*, and
51returns an accordingly opened file-like object. Two useful hooks are already
52provided by this module.
53
54The following function is the primary interface of this module:
55
56
Georg Brandl71515ca2009-05-17 12:29:12 +000057.. function:: input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
Georg Brandl116aa622007-08-15 14:28:22 +000058
59 Create an instance of the :class:`FileInput` class. The instance will be used
60 as global state for the functions of this module, and is also returned to use
61 during iteration. The parameters to this function will be passed along to the
62 constructor of the :class:`FileInput` class.
63
Georg Brandl6cb7b652010-07-31 20:08:15 +000064 The :class:`FileInput` instance can be used as a context manager in the
65 :keyword:`with` statement. In this example, *input* is closed after the
66 :keyword:`with` statement is exited, even if an exception occurs::
67
Raymond Hettinger7fefaff2010-09-05 23:50:32 +000068 with fileinput.input(files=('spam.txt', 'eggs.txt')) as f:
69 for line in f:
70 process(line)
Georg Brandl6cb7b652010-07-31 20:08:15 +000071
72 .. versionchanged:: 3.2
73 Can be used as a context manager.
74
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +020075 .. versionchanged:: 3.5.2
76 The *bufsize* parameter is no longer used.
77
Georg Brandl116aa622007-08-15 14:28:22 +000078
79The following functions use the global state created by :func:`fileinput.input`;
80if there is no active state, :exc:`RuntimeError` is raised.
81
82
83.. function:: filename()
84
85 Return the name of the file currently being read. Before the first line has
86 been read, returns ``None``.
87
88
89.. function:: fileno()
90
91 Return the integer "file descriptor" for the current file. When no file is
92 opened (before the first line and between files), returns ``-1``.
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
95.. function:: lineno()
96
97 Return the cumulative line number of the line that has just been read. Before
98 the first line has been read, returns ``0``. After the last line of the last
99 file has been read, returns the line number of that line.
100
101
102.. function:: filelineno()
103
104 Return the line number in the current file. Before the first line has been
105 read, returns ``0``. After the last line of the last file has been read,
106 returns the line number of that line within the file.
107
108
109.. function:: isfirstline()
110
111 Returns true if the line just read is the first line of its file, otherwise
112 returns false.
113
114
115.. function:: isstdin()
116
117 Returns true if the last line was read from ``sys.stdin``, otherwise returns
118 false.
119
120
121.. function:: nextfile()
122
123 Close the current file so that the next iteration will read the first line from
124 the next file (if any); lines not read from the file will not count towards the
125 cumulative line count. The filename is not changed until after the first line
126 of the next file has been read. Before the first line has been read, this
127 function has no effect; it cannot be used to skip the first file. After the
128 last line of the last file has been read, this function has no effect.
129
130
131.. function:: close()
132
133 Close the sequence.
134
135The class which implements the sequence behavior provided by the module is
136available for subclassing as well:
137
138
Georg Brandl71515ca2009-05-17 12:29:12 +0000139.. class:: FileInput(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141 Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
142 :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300143 :meth:`isstdin`, :meth:`nextfile` and :meth:`close` correspond to the
144 functions of the same name in the module. In addition it has a
145 :meth:`~io.TextIOBase.readline` method which returns the next input line,
146 and a :meth:`__getitem__` method which implements the sequence behavior.
147 The sequence must be accessed in strictly sequential order; random access
148 and :meth:`~io.TextIOBase.readline` cannot be mixed.
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150 With *mode* you can specify which file mode will be passed to :func:`open`. It
151 must be one of ``'r'``, ``'rU'``, ``'U'`` and ``'rb'``.
152
153 The *openhook*, when given, must be a function that takes two arguments,
154 *filename* and *mode*, and returns an accordingly opened file-like object. You
155 cannot use *inplace* and *openhook* together.
156
Georg Brandl6cb7b652010-07-31 20:08:15 +0000157 A :class:`FileInput` instance can be used as a context manager in the
158 :keyword:`with` statement. In this example, *input* is closed after the
159 :keyword:`with` statement is exited, even if an exception occurs::
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl6cb7b652010-07-31 20:08:15 +0000161 with FileInput(files=('spam.txt', 'eggs.txt')) as input:
162 process(input)
163
164 .. versionchanged:: 3.2
165 Can be used as a context manager.
166
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200167 .. deprecated:: 3.4
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200168 The ``'rU'`` and ``'U'`` modes.
169
170 .. versionchanged:: 3.5.2
171 The *bufsize* parameter is no longer used.
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200172
Georg Brandl6cb7b652010-07-31 20:08:15 +0000173
174**Optional in-place filtering:** if the keyword argument ``inplace=True`` is
175passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the
176file is moved to a backup file and standard output is directed to the input file
177(if a file of the same name as the backup file already exists, it will be
178replaced silently). This makes it possible to write a filter that rewrites its
179input file in place. If the *backup* parameter is given (typically as
Georg Brandl116aa622007-08-15 14:28:22 +0000180``backup='.<some extension>'``), it specifies the extension for the backup file,
181and the backup file remains around; by default, the extension is ``'.bak'`` and
182it is deleted when the output file is closed. In-place filtering is disabled
183when standard input is read.
184
Georg Brandl116aa622007-08-15 14:28:22 +0000185
186The two following opening hooks are provided by this module:
187
Georg Brandl116aa622007-08-15 14:28:22 +0000188.. function:: hook_compressed(filename, mode)
189
190 Transparently opens files compressed with gzip and bzip2 (recognized by the
191 extensions ``'.gz'`` and ``'.bz2'``) using the :mod:`gzip` and :mod:`bz2`
192 modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is
193 opened normally (ie, using :func:`open` without any decompression).
194
195 Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
196
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198.. function:: hook_encoded(encoding)
199
Serhiy Storchakaa87e6ba2016-04-27 23:06:15 +0300200 Returns a hook which opens each file with :func:`open`, using the given
Georg Brandl116aa622007-08-15 14:28:22 +0000201 *encoding* to read the file.
202
203 Usage example: ``fi =
204 fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``