Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
fileinput Namespace Reference

Data Structures

class  FileInput
 

Functions

def input
 
def close
 
def nextfile
 
def filename
 
def lineno
 
def filelineno
 
def isfirstline
 
def isstdin
 

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.  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
- specify open mode ('r' or 'rb')
- fileno()
- isatty()
- read(), read(size), even readlines()

Function Documentation

def fileinput.close ( )

Definition at line 98 of file fileinput.py.

98 
99 def close():
100  global _state
101  state = _state
102  _state = None
103  if state:
104  state.close()
def fileinput.filelineno ( )

Definition at line 120 of file fileinput.py.

121 def filelineno():
122  if not _state:
123  raise RuntimeError, "no active input()"
124  return _state.filelineno()
def fileinput.filename ( )

Definition at line 110 of file fileinput.py.

111 def filename():
112  if not _state:
113  raise RuntimeError, "no active input()"
114  return _state.filename()
def fileinput.input (   files = None,
  inplace = 0,
  backup = "",
  bufsize = 0 
)

Definition at line 91 of file fileinput.py.

91 
92 def input(files=None, inplace=0, backup="", bufsize=0):
93  global _state
94  if _state and _state._file:
95  raise RuntimeError, "input() already active"
96  _state = FileInput(files, inplace, backup, bufsize)
97  return _state
def fileinput.isfirstline ( )

Definition at line 125 of file fileinput.py.

126 def isfirstline():
127  if not _state:
128  raise RuntimeError, "no active input()"
129  return _state.isfirstline()
def fileinput.isstdin ( )

Definition at line 130 of file fileinput.py.

131 def isstdin():
132  if not _state:
133  raise RuntimeError, "no active input()"
134  return _state.isstdin()
def fileinput.lineno ( )

Definition at line 115 of file fileinput.py.

116 def lineno():
117  if not _state:
118  raise RuntimeError, "no active input()"
119  return _state.lineno()
def fileinput.nextfile ( )

Definition at line 105 of file fileinput.py.

106 def nextfile():
107  if not _state:
108  raise RuntimeError, "no active input()"
109  return _state.nextfile()

Variable Documentation

list __all__
Initial value:
1 = ["input","close","nextfile","filename","lineno","filelineno",
2  "isfirstline","isstdin","FileInput"]

Definition at line 84 of file fileinput.py.

_state = None

Definition at line 87 of file fileinput.py.

int DEFAULT_BUFSIZE = 8

Definition at line 89 of file fileinput.py.