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