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