Back to index

python3.2  3.2.2
Public Member Functions | Private Attributes
fileinput.FileInput Class Reference

List of all members.

Public Member Functions

def __init__
def __del__
def close
def __enter__
def __exit__
def __iter__
def __next__
def __getitem__
def nextfile
def readline
def filename
def lineno
def filelineno
def fileno
def isfirstline
def isstdin

Private Attributes

 _files
 _inplace
 _backup
 _bufsize
 _savestdout
 _output
 _filename
 _lineno
 _filelineno
 _file
 _isstdin
 _backupfilename
 _buffer
 _bufindex
 _mode
 _openhook

Detailed Description

class FileInput([files[, inplace[, backup[, mode[, openhook]]]]])

Class FileInput is the implementation of the module; its methods
filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
nextfile() and close() correspond to the functions of the same name
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; random access and readline() cannot be mixed.

Definition at line 185 of file fileinput.py.


Constructor & Destructor Documentation

def fileinput.FileInput.__init__ (   self,
  files = None,
  inplace = False,
  backup = "",
  bufsize = 0,
  mode = "r",
  openhook = None 
)

Definition at line 199 of file fileinput.py.

00199 
00200                  mode="r", openhook=None):
00201         if isinstance(files, str):
00202             files = (files,)
00203         else:
00204             if files is None:
00205                 files = sys.argv[1:]
00206             if not files:
00207                 files = ('-',)
00208             else:
00209                 files = tuple(files)
00210         self._files = files
00211         self._inplace = inplace
00212         self._backup = backup
00213         self._bufsize = bufsize or DEFAULT_BUFSIZE
00214         self._savestdout = None
00215         self._output = None
00216         self._filename = None
00217         self._lineno = 0
00218         self._filelineno = 0
00219         self._file = None
00220         self._isstdin = False
00221         self._backupfilename = None
00222         self._buffer = []
00223         self._bufindex = 0
00224         # restrict mode argument to reading modes
00225         if mode not in ('r', 'rU', 'U', 'rb'):
00226             raise ValueError("FileInput opening mode must be one of "
00227                              "'r', 'rU', 'U' and 'rb'")
00228         self._mode = mode
00229         if inplace and openhook:
00230             raise ValueError("FileInput cannot use an opening hook in inplace mode")
00231         elif openhook and not hasattr(openhook, '__call__'):
00232             raise ValueError("FileInput openhook must be callable")
00233         self._openhook = openhook

Here is the caller graph for this function:

Definition at line 234 of file fileinput.py.

00234 
00235     def __del__(self):
00236         self.close()

Here is the call graph for this function:


Member Function Documentation

Definition at line 241 of file fileinput.py.

00241 
00242     def __enter__(self):
00243         return self

def fileinput.FileInput.__exit__ (   self,
  type,
  value,
  traceback 
)

Definition at line 244 of file fileinput.py.

00244 
00245     def __exit__(self, type, value, traceback):
00246         self.close()

Here is the call graph for this function:

Here is the caller graph for this function:

def fileinput.FileInput.__getitem__ (   self,
  i 
)

Definition at line 265 of file fileinput.py.

00265 
00266     def __getitem__(self, i):
00267         if i != self._lineno:
00268             raise RuntimeError("accessing lines out of order")
00269         try:
00270             return self.__next__()
00271         except StopIteration:
00272             raise IndexError("end of input reached")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 247 of file fileinput.py.

00247 
00248     def __iter__(self):
00249         return self

Definition at line 250 of file fileinput.py.

00250 
00251     def __next__(self):
00252         try:
00253             line = self._buffer[self._bufindex]
00254         except IndexError:
00255             pass
00256         else:
00257             self._bufindex += 1
00258             self._lineno += 1
00259             self._filelineno += 1
00260             return line
00261         line = self.readline()
00262         if not line:
00263             raise StopIteration
00264         return line

Here is the call graph for this function:

Here is the caller graph for this function:

def fileinput.FileInput.close (   self)

Definition at line 237 of file fileinput.py.

00237 
00238     def close(self):
00239         self.nextfile()
00240         self._files = ()

Here is the call graph for this function:

Definition at line 368 of file fileinput.py.

00368 
00369     def filelineno(self):
00370         return self._filelineno

Definition at line 362 of file fileinput.py.

00362 
00363     def filename(self):
00364         return self._filename

Here is the caller graph for this function:

Definition at line 371 of file fileinput.py.

00371 
00372     def fileno(self):
00373         if self._file:
00374             try:
00375                 return self._file.fileno()
00376             except ValueError:
00377                 return -1
00378         else:
00379             return -1

Here is the caller graph for this function:

Definition at line 380 of file fileinput.py.

00380 
00381     def isfirstline(self):
00382         return self._filelineno == 1

Definition at line 383 of file fileinput.py.

00383 
00384     def isstdin(self):
00385         return self._isstdin
00386 

Definition at line 365 of file fileinput.py.

00365 
00366     def lineno(self):
00367         return self._lineno

Here is the caller graph for this function:

Definition at line 273 of file fileinput.py.

00273 
00274     def nextfile(self):
00275         savestdout = self._savestdout
00276         self._savestdout = 0
00277         if savestdout:
00278             sys.stdout = savestdout
00279 
00280         output = self._output
00281         self._output = 0
00282         if output:
00283             output.close()
00284 
00285         file = self._file
00286         self._file = 0
00287         if file and not self._isstdin:
00288             file.close()
00289 
00290         backupfilename = self._backupfilename
00291         self._backupfilename = 0
00292         if backupfilename and not self._backup:
00293             try: os.unlink(backupfilename)
00294             except OSError: pass
00295 
00296         self._isstdin = False
00297         self._buffer = []
00298         self._bufindex = 0

Here is the caller graph for this function:

Definition at line 299 of file fileinput.py.

00299 
00300     def readline(self):
00301         try:
00302             line = self._buffer[self._bufindex]
00303         except IndexError:
00304             pass
00305         else:
00306             self._bufindex += 1
00307             self._lineno += 1
00308             self._filelineno += 1
00309             return line
00310         if not self._file:
00311             if not self._files:
00312                 return ""
00313             self._filename = self._files[0]
00314             self._files = self._files[1:]
00315             self._filelineno = 0
00316             self._file = None
00317             self._isstdin = False
00318             self._backupfilename = 0
00319             if self._filename == '-':
00320                 self._filename = '<stdin>'
00321                 self._file = sys.stdin
00322                 self._isstdin = True
00323             else:
00324                 if self._inplace:
00325                     self._backupfilename = (
00326                         self._filename + (self._backup or ".bak"))
00327                     try: os.unlink(self._backupfilename)
00328                     except os.error: pass
00329                     # The next few lines may raise IOError
00330                     os.rename(self._filename, self._backupfilename)
00331                     self._file = open(self._backupfilename, self._mode)
00332                     try:
00333                         perm = os.fstat(self._file.fileno()).st_mode
00334                     except OSError:
00335                         self._output = open(self._filename, "w")
00336                     else:
00337                         mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
00338                         if hasattr(os, 'O_BINARY'):
00339                             mode |= os.O_BINARY
00340 
00341                         fd = os.open(self._filename, mode, perm)
00342                         self._output = os.fdopen(fd, "w")
00343                         try:
00344                             if hasattr(os, 'chmod'):
00345                                 os.chmod(self._filename, perm)
00346                         except OSError:
00347                             pass
00348                     self._savestdout = sys.stdout
00349                     sys.stdout = self._output
00350                 else:
00351                     # This may raise IOError
00352                     if self._openhook:
00353                         self._file = self._openhook(self._filename, self._mode)
00354                     else:
00355                         self._file = open(self._filename, self._mode)
00356         self._buffer = self._file.readlines(self._bufsize)
00357         self._bufindex = 0
00358         if not self._buffer:
00359             self.nextfile()
00360         # Recursive call
00361         return self.readline()

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 211 of file fileinput.py.

Definition at line 220 of file fileinput.py.

Definition at line 221 of file fileinput.py.

Definition at line 222 of file fileinput.py.

Definition at line 212 of file fileinput.py.

Definition at line 218 of file fileinput.py.

Definition at line 217 of file fileinput.py.

Definition at line 215 of file fileinput.py.

Definition at line 209 of file fileinput.py.

Definition at line 210 of file fileinput.py.

Definition at line 219 of file fileinput.py.

Definition at line 216 of file fileinput.py.

Definition at line 227 of file fileinput.py.

Definition at line 232 of file fileinput.py.

Definition at line 214 of file fileinput.py.

Definition at line 213 of file fileinput.py.


The documentation for this class was generated from the following file: