Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / module-fileinput.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-calendar.html" />
<link rel="prev" href="module-ConfigParser.html" />
<link rel="parent" href="misc.html" />
<link rel="next" href="module-calendar.html" />
<meta name='aesop' content='information' />
<title>5.18 fileinput -- Iterate over lines from multiple input streams</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.17.3 SafeConfigParser Objects"
href="SafeConfigParser-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.19 calendar "
href="module-calendar.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="SafeConfigParser-objects.html">5.17.3 SafeConfigParser Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-calendar.html">5.19 calendar </A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION0071800000000000000000">
5.18 <tt class="module">fileinput</tt> --
Iterate over lines from multiple input streams</A>
</H1>
<A NAME="module-fileinput"></A>
<P>
<P>
This module implements a helper class and functions to quickly write a
loop over standard input or a list of files.
<P>
The typical use is:
<P>
<div class="verbatim"><pre>
import fileinput
for line in fileinput.input():
process(line)
</pre></div>
<P>
This iterates over the lines of all files listed in
<code>sys.argv[1:]</code>, defaulting to <code>sys.stdin</code> if the list is
empty. If a filename is <code>'-'</code>, it is also replaced by
<code>sys.stdin</code>. To specify an alternative list of filenames, pass
it as the first argument to <tt class="function">input()</tt>. A single file name is
also allowed.
<P>
All files are opened in text mode. If an I/O error occurs during
opening or reading a file, <tt class="exception">IOError</tt> is raised.
<P>
If <code>sys.stdin</code> is used more than once, the second and further use
will return no lines, except perhaps for interactive use, or if it has
been explicitly reset (e.g. using <code>sys.stdin.seek(0)</code>).
<P>
Empty files are opened and immediately closed; the only time their
presence in the list of filenames is noticeable at all is when the
last file opened is empty.
<P>
It is possible that the last line of a file does not end in a newline
character; lines are returned including the trailing newline when it
is present.
<P>
The following function is the primary interface of this module:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1436' xml:id='l2h-1436' class="function">input</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>files</var><big>[</big><var>,
inplace</var><big>[</big><var>, backup</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Create an instance of the <tt class="class">FileInput</tt> class. The instance
will be used as global state for the functions of this module, and
is also returned to use during iteration. The parameters to this
function will be passed along to the constructor of the
<tt class="class">FileInput</tt> class.
</dl>
<P>
The following functions use the global state created by
<tt class="function">input()</tt>; if there is no active state,
<tt class="exception">RuntimeError</tt> is raised.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1437' xml:id='l2h-1437' class="function">filename</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the name of the file currently being read. Before the first
line has been read, returns <code>None</code>.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1438' xml:id='l2h-1438' class="function">lineno</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the cumulative line number of the line that has just been
read. Before the first line has been read, returns <code>0</code>. After
the last line of the last file has been read, returns the line
number of that line.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1439' xml:id='l2h-1439' class="function">filelineno</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the line number in the current file. Before the first line
has been read, returns <code>0</code>. After the last line of the last
file has been read, returns the line number of that line within the
file.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1440' xml:id='l2h-1440' class="function">isfirstline</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns true if the line just read is the first line of its file,
otherwise returns false.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1441' xml:id='l2h-1441' class="function">isstdin</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Returns true if the last line was read from <code>sys.stdin</code>,
otherwise returns false.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1442' xml:id='l2h-1442' class="function">nextfile</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Close the current file so that the next iteration will read the
first line from the next file (if any); lines not read from the file
will not count towards the cumulative line count. The filename is
not changed until after the first line of the next file has been
read. Before the first line has been read, this function has no
effect; it cannot be used to skip the first file. After the last
line of the last file has been read, this function has no effect.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-1443' xml:id='l2h-1443' class="function">close</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Close the sequence.
</dl>
<P>
The class which implements the sequence behavior provided by the
module is available for subclassing as well:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1444' xml:id='l2h-1444' class="class">FileInput</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>files</var><big>[</big><var>,
inplace</var><big>[</big><var>, backup</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Class <tt class="class">FileInput</tt> is the implementation; its methods
<tt class="method">filename()</tt>, <tt class="method">lineno()</tt>, <tt class="method">fileline()</tt>,
<tt class="method">isfirstline()</tt>, <tt class="method">isstdin()</tt>, <tt class="method">nextfile()</tt> and
<tt class="method">close()</tt> correspond to the functions of the same name in the
module. In addition it has a <tt class="method">readline()</tt> method which
returns the next input line, and a <tt class="method">__getitem__()</tt> method
which implements the sequence behavior. The sequence must be
accessed in strictly sequential order; random access and
<tt class="method">readline()</tt> cannot be mixed.
</dl>
<P>
<strong>Optional in-place filtering:</strong> if the keyword argument
<code><var>inplace</var>=1</code> is passed to <tt class="function">input()</tt> or to the
<tt class="class">FileInput</tt> constructor, the file is moved to a backup file and
standard output is directed to the input file (if a file of the same
name as the backup file already exists, it will be replaced silently).
This makes it possible to write a filter that rewrites its input file
in place. If the keyword argument <code><var>backup</var>='.&lt;some
extension&gt;'</code> is also given, it specifies the extension for the backup
file, and the backup file remains around; by default, the extension is
<code>'.bak'</code> and it is deleted when the output file is closed. In-place
filtering is disabled when standard input is read.
<P>
<strong>Caveat:</strong> The current implementation does not work for MS-DOS
8+3 filesystems.
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.17.3 SafeConfigParser Objects"
href="SafeConfigParser-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services"
href="misc.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="5.19 calendar "
href="module-calendar.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="SafeConfigParser-objects.html">5.17.3 SafeConfigParser Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-calendar.html">5.19 calendar </A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>