blob: f9bb88a621146b10428a05a445232e7daa700259 [file] [log] [blame]
Guido van Rossum7d5b99d1997-11-21 17:12:59 +00001"""Helper class to quickly write a loop over all standard input files.
2
3Typical use is:
4
5 import fileinput
6 for line in fileinput.input():
7 process(line)
8
9This iterates over the lines of all files listed in sys.argv[1:],
10defaulting to sys.stdin if the list is empty. If a filename is '-' it
11is also replaced by sys.stdin. To specify an alternative list of
12filenames, pass it as the argument to input(). A single file name is
13also allowed.
14
15Functions filename(), lineno() return the filename and cumulative line
16number of the line that has just been read; filelineno() returns its
17line number in the current file; isfirstline() returns true iff the
18line just read is the first line of its file; isstdin() returns true
19iff the line was read from sys.stdin. Function nextfile() closes the
20current file so that the next iteration will read the first line from
21the next file (if any); lines not read from the file will not count
22towards the cumulative line count; the filename is not changed until
23after the first line of the next file has been read. Function close()
24closes the sequence.
25
26Before any lines have been read, filename() returns None and both line
27numbers are zero; nextfile() has no effect. After all lines have been
28read, filename() and the line number functions return the values
29pertaining to the last line read; nextfile() has no effect.
30
Georg Brandlc029f872006-02-19 14:12:34 +000031All files are opened in text mode by default, you can override this by
32setting the mode parameter to input() or FileInput.__init__().
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020033If an I/O error occurs during opening or reading a file, the OSError
Georg Brandlc029f872006-02-19 14:12:34 +000034exception is raised.
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000035
36If sys.stdin is used more than once, the second and further use will
37return no lines, except perhaps for interactive use, or if it has been
38explicitly reset (e.g. using sys.stdin.seek(0)).
39
40Empty files are opened and immediately closed; the only time their
41presence in the list of filenames is noticeable at all is when the
42last file opened is empty.
43
44It is possible that the last line of a file doesn't end in a newline
45character; otherwise lines are returned including the trailing
46newline.
47
48Class FileInput is the implementation; its methods filename(),
49lineno(), fileline(), isfirstline(), isstdin(), nextfile() and close()
50correspond to the functions in the module. In addition it has a
51readline() method which returns the next input line, and a
52__getitem__() method which implements the sequence behavior. The
53sequence must be accessed in strictly sequential order; sequence
54access and readline() cannot be mixed.
55
56Optional in-place filtering: if the keyword argument inplace=1 is
57passed to input() or to the FileInput constructor, the file is moved
58to a backup file and standard output is directed to the input file.
59This makes it possible to write a filter that rewrites its input file
60in place. If the keyword argument backup=".<some extension>" is also
61given, it specifies the extension for the backup file, and the backup
62file remains around; by default, the extension is ".bak" and it is
63deleted when the output file is closed. In-place filtering is
64disabled when standard input is read. XXX The current implementation
65does not work for MS-DOS 8+3 filesystems.
66
Guido van Rossum47955242001-01-05 14:44:39 +000067Performance: this module is unfortunately one of the slower ways of
68processing large numbers of input lines. Nevertheless, a significant
69speed-up has been obtained by using readlines(bufsize) instead of
70readline(). A new keyword argument, bufsize=N, is present on the
71input() function and the FileInput() class to override the default
72buffer size.
73
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000074XXX Possible additions:
75
76- optional getopt argument processing
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000077- isatty()
78- read(), read(size), even readlines()
79
80"""
81
Walter Dörwald294bbf32002-06-06 09:48:13 +000082import sys, os
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000083
Georg Brandlef0a8652009-05-17 12:22:57 +000084__all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno",
85 "isfirstline", "isstdin", "FileInput"]
Skip Montanaroeccd02a2001-01-20 23:34:12 +000086
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000087_state = None
88
Guido van Rossum47955242001-01-05 14:44:39 +000089DEFAULT_BUFSIZE = 8*1024
90
Georg Brandlef0a8652009-05-17 12:22:57 +000091def input(files=None, inplace=False, backup="", bufsize=0,
Georg Brandlc98eeed2006-02-19 14:57:47 +000092 mode="r", openhook=None):
Georg Brandlef0a8652009-05-17 12:22:57 +000093 """input(files=None, inplace=False, backup="", bufsize=0, \
94mode="r", openhook=None)
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000095
96 Create an instance of the FileInput class. The instance will be used
97 as global state for the functions of this module, and is also returned
98 to use during iteration. The parameters to this function will be passed
Tim Peters8ac14952002-05-23 15:15:30 +000099 along to the constructor of the FileInput class.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000100 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000101 global _state
102 if _state and _state._file:
Collin Winterce36ad82007-08-30 01:19:48 +0000103 raise RuntimeError("input() already active")
Georg Brandlc98eeed2006-02-19 14:57:47 +0000104 _state = FileInput(files, inplace, backup, bufsize, mode, openhook)
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000105 return _state
106
107def close():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000108 """Close the sequence."""
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000109 global _state
110 state = _state
111 _state = None
112 if state:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000113 state.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000114
115def nextfile():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000116 """
117 Close the current file so that the next iteration will read the first
118 line from the next file (if any); lines not read from the file will
119 not count towards the cumulative line count. The filename is not
120 changed until after the first line of the next file has been read.
121 Before the first line has been read, this function has no effect;
122 it cannot be used to skip the first file. After the last line of the
Tim Peters8ac14952002-05-23 15:15:30 +0000123 last file has been read, this function has no effect.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000124 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000125 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000126 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000127 return _state.nextfile()
128
129def filename():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000130 """
131 Return the name of the file currently being read.
Tim Peters8ac14952002-05-23 15:15:30 +0000132 Before the first line has been read, returns None.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000133 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000134 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000135 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000136 return _state.filename()
137
138def lineno():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000139 """
140 Return the cumulative line number of the line that has just been read.
141 Before the first line has been read, returns 0. After the last line
Tim Peters8ac14952002-05-23 15:15:30 +0000142 of the last file has been read, returns the line number of that line.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000143 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000144 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000145 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000146 return _state.lineno()
147
148def filelineno():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000149 """
150 Return the line number in the current file. Before the first line
151 has been read, returns 0. After the last line of the last file has
Tim Peters8ac14952002-05-23 15:15:30 +0000152 been read, returns the line number of that line within the file.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000153 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000154 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000155 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000156 return _state.filelineno()
157
Georg Brandl67e9fb92006-02-19 13:56:17 +0000158def fileno():
159 """
160 Return the file number of the current file. When no file is currently
161 opened, returns -1.
162 """
163 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000164 raise RuntimeError("no active input()")
Georg Brandl67e9fb92006-02-19 13:56:17 +0000165 return _state.fileno()
166
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000167def isfirstline():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000168 """
169 Returns true the line just read is the first line of its file,
Tim Peters8ac14952002-05-23 15:15:30 +0000170 otherwise returns false.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000171 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000172 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000173 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000174 return _state.isfirstline()
175
176def isstdin():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000177 """
178 Returns true if the last line was read from sys.stdin,
Tim Peters8ac14952002-05-23 15:15:30 +0000179 otherwise returns false.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000180 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000181 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000182 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000183 return _state.isstdin()
184
185class FileInput:
Georg Brandlc98eeed2006-02-19 14:57:47 +0000186 """class FileInput([files[, inplace[, backup[, mode[, openhook]]]]])
Tim Peters8ac14952002-05-23 15:15:30 +0000187
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000188 Class FileInput is the implementation of the module; its methods
Georg Brandl67e9fb92006-02-19 13:56:17 +0000189 filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
190 nextfile() and close() correspond to the functions of the same name
191 in the module.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000192 In addition it has a readline() method which returns the next
193 input line, and a __getitem__() method which implements the
194 sequence behavior. The sequence must be accessed in strictly
Tim Peters8ac14952002-05-23 15:15:30 +0000195 sequential order; random access and readline() cannot be mixed.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000196 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000197
Georg Brandlef0a8652009-05-17 12:22:57 +0000198 def __init__(self, files=None, inplace=False, backup="", bufsize=0,
Georg Brandlc98eeed2006-02-19 14:57:47 +0000199 mode="r", openhook=None):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000200 if isinstance(files, str):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000201 files = (files,)
202 else:
Guido van Rossum2516b392000-04-10 17:16:12 +0000203 if files is None:
204 files = sys.argv[1:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000205 if not files:
Guido van Rossum2516b392000-04-10 17:16:12 +0000206 files = ('-',)
207 else:
208 files = tuple(files)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000209 self._files = files
210 self._inplace = inplace
211 self._backup = backup
Guido van Rossum47955242001-01-05 14:44:39 +0000212 self._bufsize = bufsize or DEFAULT_BUFSIZE
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000213 self._savestdout = None
214 self._output = None
215 self._filename = None
216 self._lineno = 0
217 self._filelineno = 0
218 self._file = None
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000219 self._isstdin = False
Guido van Rossum0aec9fb1998-07-20 15:49:28 +0000220 self._backupfilename = None
Guido van Rossum47955242001-01-05 14:44:39 +0000221 self._buffer = []
222 self._bufindex = 0
Georg Brandlc029f872006-02-19 14:12:34 +0000223 # restrict mode argument to reading modes
224 if mode not in ('r', 'rU', 'U', 'rb'):
225 raise ValueError("FileInput opening mode must be one of "
226 "'r', 'rU', 'U' and 'rb'")
227 self._mode = mode
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200228 if openhook:
229 if inplace:
230 raise ValueError("FileInput cannot use an opening hook in inplace mode")
231 if not callable(openhook):
232 raise ValueError("FileInput openhook must be callable")
Georg Brandlc98eeed2006-02-19 14:57:47 +0000233 self._openhook = openhook
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000234
235 def __del__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000236 self.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000237
238 def close(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000239 self.nextfile()
240 self._files = ()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000241
Georg Brandl6cb7b652010-07-31 20:08:15 +0000242 def __enter__(self):
243 return self
244
245 def __exit__(self, type, value, traceback):
246 self.close()
247
Neil Schemenauer908632a2002-03-26 20:28:40 +0000248 def __iter__(self):
249 return self
250
Georg Brandla18af4e2007-04-21 15:47:16 +0000251 def __next__(self):
Guido van Rossum47955242001-01-05 14:44:39 +0000252 try:
253 line = self._buffer[self._bufindex]
254 except IndexError:
255 pass
256 else:
257 self._bufindex += 1
258 self._lineno += 1
259 self._filelineno += 1
260 return line
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000261 line = self.readline()
262 if not line:
Neil Schemenauer908632a2002-03-26 20:28:40 +0000263 raise StopIteration
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000264 return line
Tim Peters863ac442002-04-16 01:38:40 +0000265
Neil Schemenauer908632a2002-03-26 20:28:40 +0000266 def __getitem__(self, i):
267 if i != self._lineno:
Collin Winterce36ad82007-08-30 01:19:48 +0000268 raise RuntimeError("accessing lines out of order")
Neil Schemenauer908632a2002-03-26 20:28:40 +0000269 try:
Georg Brandla18af4e2007-04-21 15:47:16 +0000270 return self.__next__()
Neil Schemenauer908632a2002-03-26 20:28:40 +0000271 except StopIteration:
Collin Winterce36ad82007-08-30 01:19:48 +0000272 raise IndexError("end of input reached")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000273
274 def nextfile(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000275 savestdout = self._savestdout
276 self._savestdout = 0
277 if savestdout:
278 sys.stdout = savestdout
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000279
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000280 output = self._output
281 self._output = 0
282 if output:
283 output.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000284
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000285 file = self._file
286 self._file = 0
287 if file and not self._isstdin:
288 file.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000289
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000290 backupfilename = self._backupfilename
291 self._backupfilename = 0
292 if backupfilename and not self._backup:
293 try: os.unlink(backupfilename)
Skip Montanarocffac662002-08-14 02:58:16 +0000294 except OSError: pass
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000295
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000296 self._isstdin = False
Guido van Rossum47955242001-01-05 14:44:39 +0000297 self._buffer = []
298 self._bufindex = 0
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000299
300 def readline(self):
Guido van Rossum47955242001-01-05 14:44:39 +0000301 try:
302 line = self._buffer[self._bufindex]
303 except IndexError:
304 pass
305 else:
306 self._bufindex += 1
307 self._lineno += 1
308 self._filelineno += 1
309 return line
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000310 if not self._file:
311 if not self._files:
312 return ""
313 self._filename = self._files[0]
314 self._files = self._files[1:]
315 self._filelineno = 0
316 self._file = None
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000317 self._isstdin = False
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000318 self._backupfilename = 0
319 if self._filename == '-':
320 self._filename = '<stdin>'
321 self._file = sys.stdin
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000322 self._isstdin = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000323 else:
324 if self._inplace:
325 self._backupfilename = (
Skip Montanaro7a98be22007-08-16 14:35:24 +0000326 self._filename + (self._backup or ".bak"))
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200327 try:
328 os.unlink(self._backupfilename)
329 except OSError:
330 pass
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200331 # The next few lines may raise OSError
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000332 os.rename(self._filename, self._backupfilename)
Georg Brandlc029f872006-02-19 14:12:34 +0000333 self._file = open(self._backupfilename, self._mode)
Guido van Rossumdcb85831999-10-18 21:41:43 +0000334 try:
Walter Dörwald294bbf32002-06-06 09:48:13 +0000335 perm = os.fstat(self._file.fileno()).st_mode
Skip Montanarocffac662002-08-14 02:58:16 +0000336 except OSError:
Guido van Rossumdcb85831999-10-18 21:41:43 +0000337 self._output = open(self._filename, "w")
338 else:
Guido van Rossum6203d8f2007-10-29 17:39:59 +0000339 mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
340 if hasattr(os, 'O_BINARY'):
341 mode |= os.O_BINARY
342
343 fd = os.open(self._filename, mode, perm)
Guido van Rossumdcb85831999-10-18 21:41:43 +0000344 self._output = os.fdopen(fd, "w")
345 try:
Jack Jansen52941a82003-01-08 16:33:16 +0000346 if hasattr(os, 'chmod'):
347 os.chmod(self._filename, perm)
Skip Montanarocffac662002-08-14 02:58:16 +0000348 except OSError:
Guido van Rossumdcb85831999-10-18 21:41:43 +0000349 pass
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000350 self._savestdout = sys.stdout
351 sys.stdout = self._output
352 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200353 # This may raise OSError
Georg Brandlc98eeed2006-02-19 14:57:47 +0000354 if self._openhook:
355 self._file = self._openhook(self._filename, self._mode)
356 else:
357 self._file = open(self._filename, self._mode)
Guido van Rossum47955242001-01-05 14:44:39 +0000358 self._buffer = self._file.readlines(self._bufsize)
359 self._bufindex = 0
360 if not self._buffer:
361 self.nextfile()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000362 # Recursive call
363 return self.readline()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000364
365 def filename(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000366 return self._filename
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000367
368 def lineno(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000369 return self._lineno
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000370
371 def filelineno(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000372 return self._filelineno
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000373
Georg Brandl67e9fb92006-02-19 13:56:17 +0000374 def fileno(self):
375 if self._file:
376 try:
377 return self._file.fileno()
378 except ValueError:
379 return -1
380 else:
381 return -1
382
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000383 def isfirstline(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000384 return self._filelineno == 1
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000385
386 def isstdin(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000387 return self._isstdin
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000388
Georg Brandlc98eeed2006-02-19 14:57:47 +0000389
390def hook_compressed(filename, mode):
391 ext = os.path.splitext(filename)[1]
392 if ext == '.gz':
393 import gzip
394 return gzip.open(filename, mode)
395 elif ext == '.bz2':
396 import bz2
397 return bz2.BZ2File(filename, mode)
398 else:
399 return open(filename, mode)
400
401
402def hook_encoded(encoding):
Georg Brandlc98eeed2006-02-19 14:57:47 +0000403 def openhook(filename, mode):
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100404 return open(filename, mode, encoding=encoding)
Georg Brandlc98eeed2006-02-19 14:57:47 +0000405 return openhook
406
407
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000408def _test():
409 import getopt
Georg Brandlef0a8652009-05-17 12:22:57 +0000410 inplace = False
411 backup = False
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000412 opts, args = getopt.getopt(sys.argv[1:], "ib:")
413 for o, a in opts:
Georg Brandlef0a8652009-05-17 12:22:57 +0000414 if o == '-i': inplace = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000415 if o == '-b': backup = a
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000416 for line in input(args, inplace=inplace, backup=backup):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000417 if line[-1:] == '\n': line = line[:-1]
418 if line[-1:] == '\r': line = line[:-1]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000419 print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
420 isfirstline() and "*" or "", line))
421 print("%d: %s[%d]" % (lineno(), filename(), filelineno()))
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000422
423if __name__ == '__main__':
424 _test()