<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>5.8 fileinput --
Iteration over lines from multiple input streams.</TITLE>
<META NAME="description" CONTENT="5.8 fileinput --
Iteration over lines from multiple input streams.">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-calendar.html">
<LINK REL="previous" HREF="module-ConfigParser.html">
<LINK REL="up" HREF="misc.html">
<LINK REL="next" HREF="module-calendar.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-calendar.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="misc.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="ConfigParser-objects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-calendar.html">5.9 calendar  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="misc.html">5. Miscellaneous Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="ConfigParser-objects.html">5.7.1 ConfigParser Objects</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION007800000000000000000">
5.8 <tt class=module>fileinput</tt> --
         Iteration over lines from multiple input streams.</A>
</H1>


<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>
<dl><dd><pre class=verbatim>
import fileinput
for line in fileinput.input():
    process(line)
</pre></dl>

<P>
This iterates over the lines of all files listed in
<tt>sys.argv[1:]</tt>, defaulting to <tt>sys.stdin</tt> if the list is
empty.  If a filename is <tt>'-'</tt>, it is also replaced by
<tt>sys.stdin</tt>.  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 <tt>sys.stdin</tt> 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 <tt>sys.stdin.seek(0)</tt>).

<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><b><a name="l2h-813"><tt class=function>input</tt></a></b> (<big>[</big><var>files</var><big>[</big><var>,
                       inplace</var><big>[</big><var>, backup</var><big>]</big><big>]</big><big>]</big>)
<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.
</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><b><a name="l2h-814"><tt class=function>filename</tt></a></b> ()
<dd>
  Return the name of the file currently being read.  Before the first
  line has been read, returns <tt>None</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-815"><tt class=function>lineno</tt></a></b> ()
<dd>
  Return the cumulative line number of the line that has just been
  read.  Before the first line has been read, returns <tt>0</tt>.  After
  the last line of the last file has been read, returns the line
  number of that line.
</dl>

<P>
<dl><dt><b><a name="l2h-816"><tt class=function>filelineno</tt></a></b> ()
<dd>
  Return the line number in the current file.  Before the first line
  has been read, returns <tt>0</tt>.  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><b><a name="l2h-817"><tt class=function>isfirstline</tt></a></b> ()
<dd>
  Return true iff the line just read is the first line of its file.
</dl>

<P>
<dl><dt><b><a name="l2h-818"><tt class=function>isstdin</tt></a></b> ()
<dd>
  Returns true iff the last line was read from <tt>sys.stdin</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-819"><tt class=function>nextfile</tt></a></b> ()
<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><b><a name="l2h-820"><tt class=function>close</tt></a></b> ()
<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><b><a name="l2h-821"><tt class=class>FileInput</tt></a></b> (<big>[</big><var>files</var><big>[</big><var>,
                             inplace</var><big>[</big><var>, backup</var><big>]</big><big>]</big><big>]</big>)
<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>
<b>Optional in-place filtering:</b> if the keyword argument
<tt><var>inplace</var>=1</tt> 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.
This makes it possible to write a filter that rewrites its input file
in place.  If the keyword argument <tt><var>backup</var>='.&lt;some
extension&gt;'</tt> is also given, it specifies the extension for the backup
file, and the backup file remains around; by default, the extension is
<tt>'.bak'</tt> and it is deleted when the output file is closed.  In-place
filtering is disabled when standard input is read.

<P>
<b>Caveat:</b> The current implementation does not work for MS-DOS
8+3 filesystems.

<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-calendar.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="misc.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="ConfigParser-objects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-calendar.html">5.9 calendar  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="misc.html">5. Miscellaneous Services</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="ConfigParser-objects.html">5.7.1 ConfigParser Objects</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
