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