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