blob: ee06830ad8e7640d22ff9fd8b1c7866d54cbf4e1 [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.
6.. moduleauthor:: Guido van Rossum <guido@python.org>
7.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
Raymond Hettinger10480942011-01-10 03:26:08 +00009**Source code:** :source:`Lib/fileinput.py`
Georg Brandl116aa622007-08-15 14:28:22 +000010
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000011--------------
12
Thomas Wouters1b7f8912007-09-19 03:06:30 +000013This module implements a helper class and functions to quickly write a
14loop over standard input or a list of files. If you just want to read or
15write one file see :func:`open`.
Georg Brandl116aa622007-08-15 14:28:22 +000016
17The typical use is::
18
19 import fileinput
20 for line in fileinput.input():
21 process(line)
22
23This iterates over the lines of all files listed in ``sys.argv[1:]``, defaulting
24to ``sys.stdin`` if the list is empty. If a filename is ``'-'``, it is also
25replaced by ``sys.stdin``. To specify an alternative list of filenames, pass it
Georg Brandl96593ed2007-09-07 14:15:41 +000026as the first argument to :func:`.input`. A single file name is also allowed.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28All files are opened in text mode by default, but you can override this by
Georg Brandl96593ed2007-09-07 14:15:41 +000029specifying the *mode* parameter in the call to :func:`.input` or
Georg Brandl6cb7b652010-07-31 20:08:15 +000030:class:`FileInput`. If an I/O error occurs during opening or reading a file,
Antoine Pitrou4272d6a2011-10-12 19:10:10 +020031:exc:`OSError` is raised.
32
33.. versionchanged:: 3.3
34 :exc:`IOError` used to be raised; it is now an alias of :exc:`OSError`.
Georg Brandl116aa622007-08-15 14:28:22 +000035
36If ``sys.stdin`` is used more than once, the second and further use will return
37no lines, except perhaps for interactive use, or if it has been explicitly reset
38(e.g. using ``sys.stdin.seek(0)``).
39
40Empty files are opened and immediately closed; the only time their presence in
41the list of filenames is noticeable at all is when the last file opened is
42empty.
43
44Lines are returned with any newlines intact, which means that the last line in
45a file may not have one.
46
47You can control how files are opened by providing an opening hook via the
48*openhook* parameter to :func:`fileinput.input` or :class:`FileInput()`. The
49hook must be a function that takes two arguments, *filename* and *mode*, and
50returns an accordingly opened file-like object. Two useful hooks are already
51provided by this module.
52
53The following function is the primary interface of this module:
54
55
Georg Brandl71515ca2009-05-17 12:29:12 +000056.. function:: input(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
Georg Brandl116aa622007-08-15 14:28:22 +000057
58 Create an instance of the :class:`FileInput` class. The instance will be used
59 as global state for the functions of this module, and is also returned to use
60 during iteration. The parameters to this function will be passed along to the
61 constructor of the :class:`FileInput` class.
62
Georg Brandl6cb7b652010-07-31 20:08:15 +000063 The :class:`FileInput` instance can be used as a context manager in the
64 :keyword:`with` statement. In this example, *input* is closed after the
65 :keyword:`with` statement is exited, even if an exception occurs::
66
Raymond Hettinger7fefaff2010-09-05 23:50:32 +000067 with fileinput.input(files=('spam.txt', 'eggs.txt')) as f:
68 for line in f:
69 process(line)
Georg Brandl6cb7b652010-07-31 20:08:15 +000070
71 .. versionchanged:: 3.2
72 Can be used as a context manager.
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
75The following functions use the global state created by :func:`fileinput.input`;
76if there is no active state, :exc:`RuntimeError` is raised.
77
78
79.. function:: filename()
80
81 Return the name of the file currently being read. Before the first line has
82 been read, returns ``None``.
83
84
85.. function:: fileno()
86
87 Return the integer "file descriptor" for the current file. When no file is
88 opened (before the first line and between files), returns ``-1``.
89
Georg Brandl116aa622007-08-15 14:28:22 +000090
91.. function:: lineno()
92
93 Return the cumulative line number of the line that has just been read. Before
94 the first line has been read, returns ``0``. After the last line of the last
95 file has been read, returns the line number of that line.
96
97
98.. function:: filelineno()
99
100 Return the line number in the current file. Before the first line has been
101 read, returns ``0``. After the last line of the last file has been read,
102 returns the line number of that line within the file.
103
104
105.. function:: isfirstline()
106
107 Returns true if the line just read is the first line of its file, otherwise
108 returns false.
109
110
111.. function:: isstdin()
112
113 Returns true if the last line was read from ``sys.stdin``, otherwise returns
114 false.
115
116
117.. function:: nextfile()
118
119 Close the current file so that the next iteration will read the first line from
120 the next file (if any); lines not read from the file will not count towards the
121 cumulative line count. The filename is not changed until after the first line
122 of the next file has been read. Before the first line has been read, this
123 function has no effect; it cannot be used to skip the first file. After the
124 last line of the last file has been read, this function has no effect.
125
126
127.. function:: close()
128
129 Close the sequence.
130
131The class which implements the sequence behavior provided by the module is
132available for subclassing as well:
133
134
Georg Brandl71515ca2009-05-17 12:29:12 +0000135.. class:: FileInput(files=None, inplace=False, backup='', bufsize=0, mode='r', openhook=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137 Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
138 :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300139 :meth:`isstdin`, :meth:`nextfile` and :meth:`close` correspond to the
140 functions of the same name in the module. In addition it has a
141 :meth:`~io.TextIOBase.readline` method which returns the next input line,
142 and a :meth:`__getitem__` method which implements the sequence behavior.
143 The sequence must be accessed in strictly sequential order; random access
144 and :meth:`~io.TextIOBase.readline` cannot be mixed.
Georg Brandl116aa622007-08-15 14:28:22 +0000145
146 With *mode* you can specify which file mode will be passed to :func:`open`. It
147 must be one of ``'r'``, ``'rU'``, ``'U'`` and ``'rb'``.
148
149 The *openhook*, when given, must be a function that takes two arguments,
150 *filename* and *mode*, and returns an accordingly opened file-like object. You
151 cannot use *inplace* and *openhook* together.
152
Georg Brandl6cb7b652010-07-31 20:08:15 +0000153 A :class:`FileInput` instance can be used as a context manager in the
154 :keyword:`with` statement. In this example, *input* is closed after the
155 :keyword:`with` statement is exited, even if an exception occurs::
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Georg Brandl6cb7b652010-07-31 20:08:15 +0000157 with FileInput(files=('spam.txt', 'eggs.txt')) as input:
158 process(input)
159
160 .. versionchanged:: 3.2
161 Can be used as a context manager.
162
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200163 .. deprecated:: 3.4
164 The ``'rU'`` and ``'U'`` modes.
165
Georg Brandl6cb7b652010-07-31 20:08:15 +0000166
167**Optional in-place filtering:** if the keyword argument ``inplace=True`` is
168passed to :func:`fileinput.input` or to the :class:`FileInput` constructor, the
169file is moved to a backup file and standard output is directed to the input file
170(if a file of the same name as the backup file already exists, it will be
171replaced silently). This makes it possible to write a filter that rewrites its
172input file in place. If the *backup* parameter is given (typically as
Georg Brandl116aa622007-08-15 14:28:22 +0000173``backup='.<some extension>'``), it specifies the extension for the backup file,
174and the backup file remains around; by default, the extension is ``'.bak'`` and
175it is deleted when the output file is closed. In-place filtering is disabled
176when standard input is read.
177
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179The two following opening hooks are provided by this module:
180
Georg Brandl116aa622007-08-15 14:28:22 +0000181.. function:: hook_compressed(filename, mode)
182
183 Transparently opens files compressed with gzip and bzip2 (recognized by the
184 extensions ``'.gz'`` and ``'.bz2'``) using the :mod:`gzip` and :mod:`bz2`
185 modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is
186 opened normally (ie, using :func:`open` without any decompression).
187
188 Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
189
Georg Brandl116aa622007-08-15 14:28:22 +0000190
191.. function:: hook_encoded(encoding)
192
193 Returns a hook which opens each file with :func:`codecs.open`, using the given
194 *encoding* to read the file.
195
196 Usage example: ``fi =
197 fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``