Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{fileinput} --- |
Fred Drake | 8ad2703 | 1999-06-29 16:00:22 +0000 | [diff] [blame] | 2 | Iterate over lines from multiple input streams} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{fileinput} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 4 | \moduleauthor{Guido van Rossum}{guido@python.org} |
| 5 | \sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 7 | \modulesynopsis{Perl-like iteration over lines from multiple input |
| 8 | streams, with ``save in place'' capability.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 9 | |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 10 | |
| 11 | This module implements a helper class and functions to quickly write a |
| 12 | loop over standard input or a list of files. |
| 13 | |
| 14 | The typical use is: |
| 15 | |
| 16 | \begin{verbatim} |
| 17 | import fileinput |
| 18 | for line in fileinput.input(): |
| 19 | process(line) |
| 20 | \end{verbatim} |
| 21 | |
| 22 | This iterates over the lines of all files listed in |
| 23 | \code{sys.argv[1:]}, defaulting to \code{sys.stdin} if the list is |
| 24 | empty. If a filename is \code{'-'}, it is also replaced by |
| 25 | \code{sys.stdin}. To specify an alternative list of filenames, pass |
| 26 | it as the first argument to \function{input()}. A single file name is |
| 27 | also allowed. |
| 28 | |
| 29 | All files are opened in text mode. If an I/O error occurs during |
| 30 | opening or reading a file, \exception{IOError} is raised. |
| 31 | |
| 32 | If \code{sys.stdin} is used more than once, the second and further use |
| 33 | will return no lines, except perhaps for interactive use, or if it has |
| 34 | been explicitly reset (e.g. using \code{sys.stdin.seek(0)}). |
| 35 | |
| 36 | Empty files are opened and immediately closed; the only time their |
| 37 | presence in the list of filenames is noticeable at all is when the |
| 38 | last file opened is empty. |
| 39 | |
| 40 | It is possible that the last line of a file does not end in a newline |
| 41 | character; lines are returned including the trailing newline when it |
| 42 | is present. |
| 43 | |
| 44 | The following function is the primary interface of this module: |
| 45 | |
| 46 | \begin{funcdesc}{input}{\optional{files\optional{, |
| 47 | inplace\optional{, backup}}}} |
| 48 | Create an instance of the \class{FileInput} class. The instance |
| 49 | will be used as global state for the functions of this module, and |
Fred Drake | 1ef24e1 | 2001-05-09 03:24:55 +0000 | [diff] [blame] | 50 | is also returned to use during iteration. The parameters to this |
| 51 | function will be passed along to the constructor of the |
| 52 | \class{FileInput} class. |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 53 | \end{funcdesc} |
| 54 | |
| 55 | |
| 56 | The following functions use the global state created by |
| 57 | \function{input()}; if there is no active state, |
| 58 | \exception{RuntimeError} is raised. |
| 59 | |
| 60 | \begin{funcdesc}{filename}{} |
| 61 | Return the name of the file currently being read. Before the first |
| 62 | line has been read, returns \code{None}. |
| 63 | \end{funcdesc} |
| 64 | |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame^] | 65 | \begin{funcdesc}{fileno}{} |
| 66 | Return the integer ``file descriptor'' for the current file. When no |
| 67 | file is opened (before the first line and between files), returns |
| 68 | \code{-1}. |
| 69 | \end{funcdesc} |
| 70 | |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 71 | \begin{funcdesc}{lineno}{} |
| 72 | Return the cumulative line number of the line that has just been |
| 73 | read. Before the first line has been read, returns \code{0}. After |
| 74 | the last line of the last file has been read, returns the line |
| 75 | number of that line. |
| 76 | \end{funcdesc} |
| 77 | |
| 78 | \begin{funcdesc}{filelineno}{} |
| 79 | Return the line number in the current file. Before the first line |
| 80 | has been read, returns \code{0}. After the last line of the last |
| 81 | file has been read, returns the line number of that line within the |
| 82 | file. |
| 83 | \end{funcdesc} |
| 84 | |
| 85 | \begin{funcdesc}{isfirstline}{} |
Fred Drake | dbe7980 | 2003-11-10 14:43:16 +0000 | [diff] [blame] | 86 | Returns true if the line just read is the first line of its file, |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 87 | otherwise returns false. |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 88 | \end{funcdesc} |
| 89 | |
| 90 | \begin{funcdesc}{isstdin}{} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 91 | Returns true if the last line was read from \code{sys.stdin}, |
| 92 | otherwise returns false. |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 93 | \end{funcdesc} |
| 94 | |
| 95 | \begin{funcdesc}{nextfile}{} |
| 96 | Close the current file so that the next iteration will read the |
| 97 | first line from the next file (if any); lines not read from the file |
| 98 | will not count towards the cumulative line count. The filename is |
| 99 | not changed until after the first line of the next file has been |
| 100 | read. Before the first line has been read, this function has no |
| 101 | effect; it cannot be used to skip the first file. After the last |
| 102 | line of the last file has been read, this function has no effect. |
| 103 | \end{funcdesc} |
| 104 | |
| 105 | \begin{funcdesc}{close}{} |
| 106 | Close the sequence. |
| 107 | \end{funcdesc} |
| 108 | |
| 109 | |
| 110 | The class which implements the sequence behavior provided by the |
| 111 | module is available for subclassing as well: |
| 112 | |
| 113 | \begin{classdesc}{FileInput}{\optional{files\optional{, |
| 114 | inplace\optional{, backup}}}} |
| 115 | Class \class{FileInput} is the implementation; its methods |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame^] | 116 | \method{filename()}, \method{fileno()}, \method{lineno()}, |
| 117 | \method{fileline()}, \method{isfirstline()}, \method{isstdin()}, |
| 118 | \method{nextfile()} and \method{close()} correspond to the functions |
| 119 | of the same name in the module. |
| 120 | In addition it has a \method{readline()} method which |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 121 | returns the next input line, and a \method{__getitem__()} method |
| 122 | which implements the sequence behavior. The sequence must be |
| 123 | accessed in strictly sequential order; random access and |
| 124 | \method{readline()} cannot be mixed. |
| 125 | \end{classdesc} |
| 126 | |
| 127 | \strong{Optional in-place filtering:} if the keyword argument |
| 128 | \code{\var{inplace}=1} is passed to \function{input()} or to the |
| 129 | \class{FileInput} constructor, the file is moved to a backup file and |
Fred Drake | 1ef24e1 | 2001-05-09 03:24:55 +0000 | [diff] [blame] | 130 | standard output is directed to the input file (if a file of the same |
| 131 | name as the backup file already exists, it will be replaced silently). |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 132 | This makes it possible to write a filter that rewrites its input file |
| 133 | in place. If the keyword argument \code{\var{backup}='.<some |
| 134 | extension>'} is also given, it specifies the extension for the backup |
| 135 | file, and the backup file remains around; by default, the extension is |
| 136 | \code{'.bak'} and it is deleted when the output file is closed. In-place |
| 137 | filtering is disabled when standard input is read. |
| 138 | |
| 139 | \strong{Caveat:} The current implementation does not work for MS-DOS |
| 140 | 8+3 filesystems. |