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