blob: ba7e980ba4b67ab5c93d7b0229819b9dbe0cc7e4 [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
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
Georg Brandl116aa622007-08-15 14:28:22 +000056
57The following functions use the global state created by :func:`fileinput.input`;
58if there is no active state, :exc:`RuntimeError` is raised.
59
60
61.. function:: filename()
62
63 Return the name of the file currently being read. Before the first line has
64 been read, returns ``None``.
65
66
67.. function:: fileno()
68
69 Return the integer "file descriptor" for the current file. When no file is
70 opened (before the first line and between files), returns ``-1``.
71
Georg Brandl116aa622007-08-15 14:28:22 +000072
73.. function:: lineno()
74
75 Return the cumulative line number of the line that has just been read. Before
76 the first line has been read, returns ``0``. After the last line of the last
77 file has been read, returns the line number of that line.
78
79
80.. function:: filelineno()
81
82 Return the line number in the current file. Before the first line has been
83 read, returns ``0``. After the last line of the last file has been read,
84 returns the line number of that line within the file.
85
86
87.. function:: isfirstline()
88
89 Returns true if the line just read is the first line of its file, otherwise
90 returns false.
91
92
93.. function:: isstdin()
94
95 Returns true if the last line was read from ``sys.stdin``, otherwise returns
96 false.
97
98
99.. function:: nextfile()
100
101 Close the current file so that the next iteration will read the first line from
102 the next file (if any); lines not read from the file will not count towards the
103 cumulative line count. The filename is not changed until after the first line
104 of the next file has been read. Before the first line has been read, this
105 function has no effect; it cannot be used to skip the first file. After the
106 last line of the last file has been read, this function has no effect.
107
108
109.. function:: close()
110
111 Close the sequence.
112
113The class which implements the sequence behavior provided by the module is
114available for subclassing as well:
115
116
117.. class:: FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
118
119 Class :class:`FileInput` is the implementation; its methods :meth:`filename`,
120 :meth:`fileno`, :meth:`lineno`, :meth:`filelineno`, :meth:`isfirstline`,
121 :meth:`isstdin`, :meth:`nextfile` and :meth:`close` correspond to the functions
122 of the same name in the module. In addition it has a :meth:`readline` method
123 which returns the next input line, and a :meth:`__getitem__` method which
124 implements the sequence behavior. The sequence must be accessed in strictly
125 sequential order; random access and :meth:`readline` cannot be mixed.
126
127 With *mode* you can specify which file mode will be passed to :func:`open`. It
128 must be one of ``'r'``, ``'rU'``, ``'U'`` and ``'rb'``.
129
130 The *openhook*, when given, must be a function that takes two arguments,
131 *filename* and *mode*, and returns an accordingly opened file-like object. You
132 cannot use *inplace* and *openhook* together.
133
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135**Optional in-place filtering:** if the keyword argument ``inplace=1`` is passed
136to :func:`fileinput.input` or to the :class:`FileInput` constructor, the file is
137moved to a backup file and standard output is directed to the input file (if a
138file of the same name as the backup file already exists, it will be replaced
139silently). This makes it possible to write a filter that rewrites its input
140file in place. If the *backup* parameter is given (typically as
141``backup='.<some extension>'``), it specifies the extension for the backup file,
142and the backup file remains around; by default, the extension is ``'.bak'`` and
143it is deleted when the output file is closed. In-place filtering is disabled
144when standard input is read.
145
Guido van Rossumda27fd22007-08-17 00:24:54 +0000146.. warning::
147
148 The current implementation does not work for MS-DOS 8+3 filesystems.
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151The two following opening hooks are provided by this module:
152
Georg Brandl116aa622007-08-15 14:28:22 +0000153.. function:: hook_compressed(filename, mode)
154
155 Transparently opens files compressed with gzip and bzip2 (recognized by the
156 extensions ``'.gz'`` and ``'.bz2'``) using the :mod:`gzip` and :mod:`bz2`
157 modules. If the filename extension is not ``'.gz'`` or ``'.bz2'``, the file is
158 opened normally (ie, using :func:`open` without any decompression).
159
160 Usage example: ``fi = fileinput.FileInput(openhook=fileinput.hook_compressed)``
161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163.. function:: hook_encoded(encoding)
164
165 Returns a hook which opens each file with :func:`codecs.open`, using the given
166 *encoding* to read the file.
167
168 Usage example: ``fi =
169 fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))``
170
171 .. note::
172
173 With this hook, :class:`FileInput` might return Unicode strings depending on the
174 specified *encoding*.
175