Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / module-fileinput.html
CommitLineData
86530b38
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-calendar.html" />
13<link rel="prev" href="module-ConfigParser.html" />
14<link rel="parent" href="misc.html" />
15<link rel="next" href="module-calendar.html" />
16<meta name='aesop' content='information' />
17<title>5.18 fileinput -- Iterate over lines from multiple input streams</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="5.17.3 SafeConfigParser Objects"
25 href="SafeConfigParser-objects.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
28 href="misc.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="5.19 calendar "
31 href="module-calendar.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="SafeConfigParser-objects.html">5.17.3 SafeConfigParser Objects</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-calendar.html">5.19 calendar </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0071800000000000000000">
565.18 <tt class="module">fileinput</tt> --
57 Iterate over lines from multiple input streams</A>
58</H1>
59<A NAME="module-fileinput"></A>
60
61<P>
62
63<P>
64This module implements a helper class and functions to quickly write a
65loop over standard input or a list of files.
66
67<P>
68The typical use is:
69
70<P>
71<div class="verbatim"><pre>
72import fileinput
73for line in fileinput.input():
74 process(line)
75</pre></div>
76
77<P>
78This iterates over the lines of all files listed in
79<code>sys.argv[1:]</code>, defaulting to <code>sys.stdin</code> if the list is
80empty. If a filename is <code>'-'</code>, it is also replaced by
81<code>sys.stdin</code>. To specify an alternative list of filenames, pass
82it as the first argument to <tt class="function">input()</tt>. A single file name is
83also allowed.
84
85<P>
86All files are opened in text mode. If an I/O error occurs during
87opening or reading a file, <tt class="exception">IOError</tt> is raised.
88
89<P>
90If <code>sys.stdin</code> is used more than once, the second and further use
91will return no lines, except perhaps for interactive use, or if it has
92been explicitly reset (e.g. using <code>sys.stdin.seek(0)</code>).
93
94<P>
95Empty files are opened and immediately closed; the only time their
96presence in the list of filenames is noticeable at all is when the
97last file opened is empty.
98
99<P>
100It is possible that the last line of a file does not end in a newline
101character; lines are returned including the trailing newline when it
102is present.
103
104<P>
105The following function is the primary interface of this module:
106
107<P>
108<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
109 <td><nobr><b><tt id='l2h-1436' xml:id='l2h-1436' class="function">input</tt></b>(</nobr></td>
110 <td><var></var><big>[</big><var>files</var><big>[</big><var>,
111 inplace</var><big>[</big><var>, backup</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
112<dd>
113 Create an instance of the <tt class="class">FileInput</tt> class. The instance
114 will be used as global state for the functions of this module, and
115 is also returned to use during iteration. The parameters to this
116 function will be passed along to the constructor of the
117 <tt class="class">FileInput</tt> class.
118</dl>
119
120<P>
121The following functions use the global state created by
122<tt class="function">input()</tt>; if there is no active state,
123<tt class="exception">RuntimeError</tt> is raised.
124
125<P>
126<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
127 <td><nobr><b><tt id='l2h-1437' xml:id='l2h-1437' class="function">filename</tt></b>(</nobr></td>
128 <td><var></var>)</td></tr></table></dt>
129<dd>
130 Return the name of the file currently being read. Before the first
131 line has been read, returns <code>None</code>.
132</dl>
133
134<P>
135<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
136 <td><nobr><b><tt id='l2h-1438' xml:id='l2h-1438' class="function">lineno</tt></b>(</nobr></td>
137 <td><var></var>)</td></tr></table></dt>
138<dd>
139 Return the cumulative line number of the line that has just been
140 read. Before the first line has been read, returns <code>0</code>. After
141 the last line of the last file has been read, returns the line
142 number of that line.
143</dl>
144
145<P>
146<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
147 <td><nobr><b><tt id='l2h-1439' xml:id='l2h-1439' class="function">filelineno</tt></b>(</nobr></td>
148 <td><var></var>)</td></tr></table></dt>
149<dd>
150 Return the line number in the current file. Before the first line
151 has been read, returns <code>0</code>. After the last line of the last
152 file has been read, returns the line number of that line within the
153 file.
154</dl>
155
156<P>
157<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
158 <td><nobr><b><tt id='l2h-1440' xml:id='l2h-1440' class="function">isfirstline</tt></b>(</nobr></td>
159 <td><var></var>)</td></tr></table></dt>
160<dd>
161 Returns true if the line just read is the first line of its file,
162 otherwise returns false.
163</dl>
164
165<P>
166<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
167 <td><nobr><b><tt id='l2h-1441' xml:id='l2h-1441' class="function">isstdin</tt></b>(</nobr></td>
168 <td><var></var>)</td></tr></table></dt>
169<dd>
170 Returns true if the last line was read from <code>sys.stdin</code>,
171 otherwise returns false.
172</dl>
173
174<P>
175<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
176 <td><nobr><b><tt id='l2h-1442' xml:id='l2h-1442' class="function">nextfile</tt></b>(</nobr></td>
177 <td><var></var>)</td></tr></table></dt>
178<dd>
179 Close the current file so that the next iteration will read the
180 first line from the next file (if any); lines not read from the file
181 will not count towards the cumulative line count. The filename is
182 not changed until after the first line of the next file has been
183 read. Before the first line has been read, this function has no
184 effect; it cannot be used to skip the first file. After the last
185 line of the last file has been read, this function has no effect.
186</dl>
187
188<P>
189<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
190 <td><nobr><b><tt id='l2h-1443' xml:id='l2h-1443' class="function">close</tt></b>(</nobr></td>
191 <td><var></var>)</td></tr></table></dt>
192<dd>
193 Close the sequence.
194</dl>
195
196<P>
197The class which implements the sequence behavior provided by the
198module is available for subclassing as well:
199
200<P>
201<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
202 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1444' xml:id='l2h-1444' class="class">FileInput</tt></b>(</nobr></td>
203 <td><var></var><big>[</big><var>files</var><big>[</big><var>,
204 inplace</var><big>[</big><var>, backup</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
205<dd>
206 Class <tt class="class">FileInput</tt> is the implementation; its methods
207 <tt class="method">filename()</tt>, <tt class="method">lineno()</tt>, <tt class="method">fileline()</tt>,
208 <tt class="method">isfirstline()</tt>, <tt class="method">isstdin()</tt>, <tt class="method">nextfile()</tt> and
209 <tt class="method">close()</tt> correspond to the functions of the same name in the
210 module. In addition it has a <tt class="method">readline()</tt> method which
211 returns the next input line, and a <tt class="method">__getitem__()</tt> method
212 which implements the sequence behavior. The sequence must be
213 accessed in strictly sequential order; random access and
214 <tt class="method">readline()</tt> cannot be mixed.
215</dl>
216
217<P>
218<strong>Optional in-place filtering:</strong> if the keyword argument
219<code><var>inplace</var>=1</code> is passed to <tt class="function">input()</tt> or to the
220<tt class="class">FileInput</tt> constructor, the file is moved to a backup file and
221standard output is directed to the input file (if a file of the same
222name as the backup file already exists, it will be replaced silently).
223This makes it possible to write a filter that rewrites its input file
224in place. If the keyword argument <code><var>backup</var>='.&lt;some
225extension&gt;'</code> is also given, it specifies the extension for the backup
226file, and the backup file remains around; by default, the extension is
227<code>'.bak'</code> and it is deleted when the output file is closed. In-place
228filtering is disabled when standard input is read.
229
230<P>
231<strong>Caveat:</strong> The current implementation does not work for MS-DOS
2328+3 filesystems.
233
234<DIV CLASS="navigation">
235<div class='online-navigation'>
236<p></p><hr />
237<table align="center" width="100%" cellpadding="0" cellspacing="2">
238<tr>
239<td class='online-navigation'><a rel="prev" title="5.17.3 SafeConfigParser Objects"
240 href="SafeConfigParser-objects.html"><img src='../icons/previous.png'
241 border='0' height='32' alt='Previous Page' width='32' /></A></td>
242<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
243 href="misc.html"><img src='../icons/up.png'
244 border='0' height='32' alt='Up One Level' width='32' /></A></td>
245<td class='online-navigation'><a rel="next" title="5.19 calendar "
246 href="module-calendar.html"><img src='../icons/next.png'
247 border='0' height='32' alt='Next Page' width='32' /></A></td>
248<td align="center" width="100%">Python Library Reference</td>
249<td class='online-navigation'><a rel="contents" title="Table of Contents"
250 href="contents.html"><img src='../icons/contents.png'
251 border='0' height='32' alt='Contents' width='32' /></A></td>
252<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
253 border='0' height='32' alt='Module Index' width='32' /></a></td>
254<td class='online-navigation'><a rel="index" title="Index"
255 href="genindex.html"><img src='../icons/index.png'
256 border='0' height='32' alt='Index' width='32' /></A></td>
257</tr></table>
258<div class='online-navigation'>
259<b class="navlabel">Previous:</b>
260<a class="sectref" rel="prev" href="SafeConfigParser-objects.html">5.17.3 SafeConfigParser Objects</A>
261<b class="navlabel">Up:</b>
262<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
263<b class="navlabel">Next:</b>
264<a class="sectref" rel="next" href="module-calendar.html">5.19 calendar </A>
265</div>
266</div>
267<hr />
268<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
269</DIV>
270<!--End of Navigation Panel-->
271<ADDRESS>
272See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
273</ADDRESS>
274</BODY>
275</HTML>