Back to index

python3.2  3.2.2
Classes | Functions | Variables
fileinput Namespace Reference

Classes

class  FileInput

Functions

def input
def close
def nextfile
def filename
def lineno
def filelineno
def fileno
def isfirstline
def isstdin
def hook_compressed
def hook_encoded
def _test

Variables

list __all__
 _state = None
int DEFAULT_BUFSIZE = 8

Detailed Description

Helper class to quickly write a loop over all standard input files.

Typical use is:

    import fileinput
    for line in fileinput.input():
process(line)

This iterates over the lines of all files listed in sys.argv[1:],
defaulting to sys.stdin if the list is empty.  If a filename is '-' it
is also replaced by sys.stdin.  To specify an alternative list of
filenames, pass it as the argument to input().  A single file name is
also allowed.

Functions filename(), lineno() return the filename and cumulative line
number of the line that has just been read; filelineno() returns its
line number in the current file; isfirstline() returns true iff the
line just read is the first line of its file; isstdin() returns true
iff the line was read from sys.stdin.  Function nextfile() closes 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.  Function close()
closes the sequence.

Before any lines have been read, filename() returns None and both line
numbers are zero; nextfile() has no effect.  After all lines have been
read, filename() and the line number functions return the values
pertaining to the last line read; nextfile() has no effect.

All files are opened in text mode by default, you can override this by
setting the mode parameter to input() or FileInput.__init__().
If an I/O error occurs during opening or reading a file, the IOError
exception is raised.

If sys.stdin 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 sys.stdin.seek(0)).

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.

It is possible that the last line of a file doesn't end in a newline
character; otherwise lines are returned including the trailing
newline.

Class FileInput is the implementation; its methods filename(),
lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close()
correspond to the functions in the module.  In addition it has a
readline() method which returns the next input line, and a
__getitem__() method which implements the sequence behavior.  The
sequence must be accessed in strictly sequential order; sequence
access and readline() cannot be mixed.

Optional in-place filtering: if the keyword argument inplace=1 is
passed to input() or to the FileInput 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 backup=".<some extension>" is also
given, it specifies the extension for the backup file, and the backup
file remains around; by default, the extension is ".bak" and it is
deleted when the output file is closed.  In-place filtering is
disabled when standard input is read.  XXX The current implementation
does not work for MS-DOS 8+3 filesystems.

Performance: this module is unfortunately one of the slower ways of
processing large numbers of input lines.  Nevertheless, a significant
speed-up has been obtained by using readlines(bufsize) instead of
readline().  A new keyword argument, bufsize=N, is present on the
input() function and the FileInput() class to override the default
buffer size.

XXX Possible additions:

- optional getopt argument processing
- isatty()
- read(), read(size), even readlines()

Function Documentation

def fileinput._test ( ) [private]

Definition at line 406 of file fileinput.py.

00406 
00407 def _test():
00408     import getopt
00409     inplace = False
00410     backup = False
00411     opts, args = getopt.getopt(sys.argv[1:], "ib:")
00412     for o, a in opts:
00413         if o == '-i': inplace = True
00414         if o == '-b': backup = a
00415     for line in input(args, inplace=inplace, backup=backup):
00416         if line[-1:] == '\n': line = line[:-1]
00417         if line[-1:] == '\r': line = line[:-1]
00418         print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
00419                                    isfirstline() and "*" or "", line))
00420     print("%d: %s[%d]" % (lineno(), filename(), filelineno()))

Here is the call graph for this function:

def fileinput.close ( )
Close the sequence.

Definition at line 107 of file fileinput.py.

00107 
00108 def close():
00109     """Close the sequence."""
00110     global _state
00111     state = _state
00112     _state = None
00113     if state:
00114         state.close()

Return the line number in the current file. Before the first line
has been read, returns 0. After the last line of the last file has
been read, returns the line number of that line within the file.

Definition at line 148 of file fileinput.py.

00148 
00149 def filelineno():
00150     """
00151     Return the line number in the current file. Before the first line
00152     has been read, returns 0. After the last line of the last file has
00153     been read, returns the line number of that line within the file.
00154     """
00155     if not _state:
00156         raise RuntimeError("no active input()")
00157     return _state.filelineno()

Here is the caller graph for this function:

Return the name of the file currently being read.
Before the first line has been read, returns None.

Definition at line 129 of file fileinput.py.

00129 
00130 def filename():
00131     """
00132     Return the name of the file currently being read.
00133     Before the first line has been read, returns None.
00134     """
00135     if not _state:
00136         raise RuntimeError("no active input()")
00137     return _state.filename()

Here is the caller graph for this function:

Return the file number of the current file. When no file is currently
opened, returns -1.

Definition at line 158 of file fileinput.py.

00158 
00159 def fileno():
00160     """
00161     Return the file number of the current file. When no file is currently
00162     opened, returns -1.
00163     """
00164     if not _state:
00165         raise RuntimeError("no active input()")
00166     return _state.fileno()

Here is the caller graph for this function:

def fileinput.hook_compressed (   filename,
  mode 
)

Definition at line 387 of file fileinput.py.

00387 
00388 def hook_compressed(filename, mode):
00389     ext = os.path.splitext(filename)[1]
00390     if ext == '.gz':
00391         import gzip
00392         return gzip.open(filename, mode)
00393     elif ext == '.bz2':
00394         import bz2
00395         return bz2.BZ2File(filename, mode)
00396     else:
00397         return open(filename, mode)
00398 

Here is the call graph for this function:

def fileinput.hook_encoded (   encoding)

Definition at line 399 of file fileinput.py.

00399 
00400 def hook_encoded(encoding):
00401     import codecs
00402     def openhook(filename, mode):
00403         return codecs.open(filename, mode, encoding)
00404     return openhook
00405 

Here is the call graph for this function:

def fileinput.input (   files = None,
  inplace = False,
  backup = "",
  bufsize = 0,
  mode = "r",
  openhook = None 
)
input(files=None, inplace=False, backup="", bufsize=0, \
mode="r", openhook=None)

Create an instance of the FileInput 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 FileInput class.

Definition at line 92 of file fileinput.py.

00092 
00093           mode="r", openhook=None):
00094     """input(files=None, inplace=False, backup="", bufsize=0, \
00095 mode="r", openhook=None)
00096 
00097     Create an instance of the FileInput class. The instance will be used
00098     as global state for the functions of this module, and is also returned
00099     to use during iteration. The parameters to this function will be passed
00100     along to the constructor of the FileInput class.
00101     """
00102     global _state
00103     if _state and _state._file:
00104         raise RuntimeError("input() already active")
00105     _state = FileInput(files, inplace, backup, bufsize, mode, openhook)
00106     return _state

Here is the caller graph for this function:

Returns true the line just read is the first line of its file,
otherwise returns false.

Definition at line 167 of file fileinput.py.

00167 
00168 def isfirstline():
00169     """
00170     Returns true the line just read is the first line of its file,
00171     otherwise returns false.
00172     """
00173     if not _state:
00174         raise RuntimeError("no active input()")
00175     return _state.isfirstline()

Here is the caller graph for this function:

Returns true if the last line was read from sys.stdin,
otherwise returns false.

Definition at line 176 of file fileinput.py.

00176 
00177 def isstdin():
00178     """
00179     Returns true if the last line was read from sys.stdin,
00180     otherwise returns false.
00181     """
00182     if not _state:
00183         raise RuntimeError("no active input()")
00184     return _state.isstdin()

Return the cumulative line number of the line that has just been read.
Before the first line has been read, returns 0. After the last line
of the last file has been read, returns the line number of that line.

Definition at line 138 of file fileinput.py.

00138 
00139 def lineno():
00140     """
00141     Return the cumulative line number of the line that has just been read.
00142     Before the first line has been read, returns 0. After the last line
00143     of the last file has been read, returns the line number of that line.
00144     """
00145     if not _state:
00146         raise RuntimeError("no active input()")
00147     return _state.lineno()

Here is the caller graph for this function:

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.

Definition at line 115 of file fileinput.py.

00115 
00116 def nextfile():
00117     """
00118     Close the current file so that the next iteration will read the first
00119     line from the next file (if any); lines not read from the file will
00120     not count towards the cumulative line count. The filename is not
00121     changed until after the first line of the next file has been read.
00122     Before the first line has been read, this function has no effect;
00123     it cannot be used to skip the first file. After the last line of the
00124     last file has been read, this function has no effect.
00125     """
00126     if not _state:
00127         raise RuntimeError("no active input()")
00128     return _state.nextfile()


Variable Documentation

Initial value:
00001 ["input", "close", "nextfile", "filename", "lineno", "filelineno",
00002            "isfirstline", "isstdin", "FileInput"]

Definition at line 84 of file fileinput.py.

Definition at line 87 of file fileinput.py.

Definition at line 89 of file fileinput.py.