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 | |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 29 | All files are opened in text mode by default, but you can override this by |
| 30 | specifying the \var{mode} parameter in the call to \function{input()} |
| 31 | or \class{FileInput()}. If an I/O error occurs during opening or reading |
| 32 | a file, \exception{IOError} is raised. |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 33 | |
| 34 | If \code{sys.stdin} is used more than once, the second and further use |
| 35 | will return no lines, except perhaps for interactive use, or if it has |
| 36 | been explicitly reset (e.g. using \code{sys.stdin.seek(0)}). |
| 37 | |
| 38 | Empty files are opened and immediately closed; the only time their |
| 39 | presence in the list of filenames is noticeable at all is when the |
| 40 | last file opened is empty. |
| 41 | |
| 42 | It is possible that the last line of a file does not end in a newline |
| 43 | character; lines are returned including the trailing newline when it |
| 44 | is present. |
| 45 | |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame^] | 46 | You can control how files are opened by providing an opening hook via the |
| 47 | \var{openhook} parameter to \function{input()} or \class{FileInput()}. |
| 48 | The hook must be a function that takes two arguments, \var{filename} |
| 49 | and \var{mode}, and returns an accordingly opened file-like object. |
| 50 | Two useful hooks are already provided by this module. |
| 51 | |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 52 | The following function is the primary interface of this module: |
| 53 | |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame^] | 54 | \begin{funcdesc}{input}{\optional{files\optional{, inplace\optional{, |
| 55 | backup\optional{, mode\optional{, openhook}}}}}} |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 56 | Create an instance of the \class{FileInput} class. The instance |
| 57 | 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] | 58 | is also returned to use during iteration. The parameters to this |
| 59 | function will be passed along to the constructor of the |
| 60 | \class{FileInput} class. |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 61 | |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame^] | 62 | \versionchanged[Added the \var{mode} and \var{openhook} parameters]{2.5} |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 63 | \end{funcdesc} |
| 64 | |
| 65 | |
| 66 | The following functions use the global state created by |
| 67 | \function{input()}; if there is no active state, |
| 68 | \exception{RuntimeError} is raised. |
| 69 | |
| 70 | \begin{funcdesc}{filename}{} |
| 71 | Return the name of the file currently being read. Before the first |
| 72 | line has been read, returns \code{None}. |
| 73 | \end{funcdesc} |
| 74 | |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 75 | \begin{funcdesc}{fileno}{} |
| 76 | Return the integer ``file descriptor'' for the current file. When no |
| 77 | file is opened (before the first line and between files), returns |
| 78 | \code{-1}. |
| 79 | \end{funcdesc} |
| 80 | |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 81 | \begin{funcdesc}{lineno}{} |
| 82 | Return the cumulative line number of the line that has just been |
| 83 | read. Before the first line has been read, returns \code{0}. After |
| 84 | the last line of the last file has been read, returns the line |
| 85 | number of that line. |
| 86 | \end{funcdesc} |
| 87 | |
| 88 | \begin{funcdesc}{filelineno}{} |
| 89 | Return the line number in the current file. Before the first line |
| 90 | has been read, returns \code{0}. After the last line of the last |
| 91 | file has been read, returns the line number of that line within the |
| 92 | file. |
| 93 | \end{funcdesc} |
| 94 | |
| 95 | \begin{funcdesc}{isfirstline}{} |
Fred Drake | dbe7980 | 2003-11-10 14:43:16 +0000 | [diff] [blame] | 96 | 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] | 97 | otherwise returns false. |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 98 | \end{funcdesc} |
| 99 | |
| 100 | \begin{funcdesc}{isstdin}{} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 101 | Returns true if the last line was read from \code{sys.stdin}, |
| 102 | otherwise returns false. |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 103 | \end{funcdesc} |
| 104 | |
| 105 | \begin{funcdesc}{nextfile}{} |
| 106 | Close the current file so that the next iteration will read the |
| 107 | first line from the next file (if any); lines not read from the file |
| 108 | will not count towards the cumulative line count. The filename is |
| 109 | not changed until after the first line of the next file has been |
| 110 | read. Before the first line has been read, this function has no |
| 111 | effect; it cannot be used to skip the first file. After the last |
| 112 | line of the last file has been read, this function has no effect. |
| 113 | \end{funcdesc} |
| 114 | |
| 115 | \begin{funcdesc}{close}{} |
| 116 | Close the sequence. |
| 117 | \end{funcdesc} |
| 118 | |
| 119 | |
| 120 | The class which implements the sequence behavior provided by the |
| 121 | module is available for subclassing as well: |
| 122 | |
| 123 | \begin{classdesc}{FileInput}{\optional{files\optional{, |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame^] | 124 | inplace\optional{, backup\optional{, |
| 125 | mode\optional{, openhook}}}}}} |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 126 | Class \class{FileInput} is the implementation; its methods |
Georg Brandl | 67e9fb9 | 2006-02-19 13:56:17 +0000 | [diff] [blame] | 127 | \method{filename()}, \method{fileno()}, \method{lineno()}, |
| 128 | \method{fileline()}, \method{isfirstline()}, \method{isstdin()}, |
| 129 | \method{nextfile()} and \method{close()} correspond to the functions |
| 130 | of the same name in the module. |
| 131 | In addition it has a \method{readline()} method which |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 132 | returns the next input line, and a \method{__getitem__()} method |
| 133 | which implements the sequence behavior. The sequence must be |
| 134 | accessed in strictly sequential order; random access and |
| 135 | \method{readline()} cannot be mixed. |
Georg Brandl | c029f87 | 2006-02-19 14:12:34 +0000 | [diff] [blame] | 136 | |
| 137 | With \var{mode} you can specify which file mode will be passed to |
| 138 | \function{open()}. It must be one of \code{'r'}, \code{'rU'}, |
| 139 | \code{'U'} and \code{'rb'}. |
| 140 | |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame^] | 141 | The \var{openhook}, when given, must be a function that takes two arguments, |
| 142 | \var{filename} and \var{mode}, and returns an accordingly opened |
| 143 | file-like object. |
| 144 | You cannot use \var{inplace} and \var{openhook} together. |
| 145 | |
| 146 | \versionchanged[Added the \var{mode} and \var{openhook} parameters]{2.5} |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 147 | \end{classdesc} |
| 148 | |
| 149 | \strong{Optional in-place filtering:} if the keyword argument |
| 150 | \code{\var{inplace}=1} is passed to \function{input()} or to the |
| 151 | \class{FileInput} constructor, the file is moved to a backup file and |
Fred Drake | 1ef24e1 | 2001-05-09 03:24:55 +0000 | [diff] [blame] | 152 | standard output is directed to the input file (if a file of the same |
| 153 | name as the backup file already exists, it will be replaced silently). |
Fred Drake | 35ca0d6 | 1998-04-04 04:20:51 +0000 | [diff] [blame] | 154 | This makes it possible to write a filter that rewrites its input file |
| 155 | in place. If the keyword argument \code{\var{backup}='.<some |
| 156 | extension>'} is also given, it specifies the extension for the backup |
| 157 | file, and the backup file remains around; by default, the extension is |
| 158 | \code{'.bak'} and it is deleted when the output file is closed. In-place |
| 159 | filtering is disabled when standard input is read. |
| 160 | |
| 161 | \strong{Caveat:} The current implementation does not work for MS-DOS |
| 162 | 8+3 filesystems. |
Georg Brandl | c98eeed | 2006-02-19 14:57:47 +0000 | [diff] [blame^] | 163 | |
| 164 | |
| 165 | The two following opening hooks are provided by this module: |
| 166 | |
| 167 | \begin{funcdesc}{hook_compressed}{filename, mode} |
| 168 | Transparently opens files compressed with gzip and bzip2 using |
| 169 | the \module{gzip} and \module{bz2} modules. |
| 170 | |
| 171 | Usage example: |
| 172 | \samp{fi = fileinput.FileInput(openhook=fileinput.hook_compressed)} |
| 173 | |
| 174 | \versionadded{2.5} |
| 175 | \end{funcdesc} |
| 176 | |
| 177 | \begin{funcdesc}{hook_encoded}{encoding} |
| 178 | Returns a hook which opens each file with \function{codecs.open()}, |
| 179 | using the given \var{encoding} to read the file. |
| 180 | |
| 181 | Usage example: |
| 182 | \samp{fi = fileinput.FileInput(openhook=fileinput.hook_encoded("iso-8859-1"))} |
| 183 | |
| 184 | \note{With this hook, \class{FileInput} might return Unicode strings |
| 185 | depending on the specified \var{encoding}.} |
| 186 | \versionadded{2.5} |
| 187 | \end{funcdesc} |
| 188 | |