blob: c6fc9a1981a1fa058b0c9f91f3f58f9cd9d5d6c5 [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
67XXX Possible additions:
68
69- optional getopt argument processing
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000070- isatty()
71- read(), read(size), even readlines()
72
73"""
74
Walter Dörwald294bbf32002-06-06 09:48:13 +000075import sys, os
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000076
Georg Brandlef0a8652009-05-17 12:22:57 +000077__all__ = ["input", "close", "nextfile", "filename", "lineno", "filelineno",
Martin Panter7978e102016-01-16 06:26:54 +000078 "fileno", "isfirstline", "isstdin", "FileInput", "hook_compressed",
79 "hook_encoded"]
Skip Montanaroeccd02a2001-01-20 23:34:12 +000080
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000081_state = None
82
Georg Brandlef0a8652009-05-17 12:22:57 +000083def input(files=None, inplace=False, backup="", bufsize=0,
Georg Brandlc98eeed2006-02-19 14:57:47 +000084 mode="r", openhook=None):
Terry Jan Reedy70d2c712013-06-28 18:59:28 -040085 """Return an instance of the FileInput class, which can be iterated.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000086
Terry Jan Reedy70d2c712013-06-28 18:59:28 -040087 The parameters are passed to the constructor of the FileInput class.
88 The returned instance, in addition to being an iterator,
89 keeps global state for the functions of this module,.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000090 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000091 global _state
92 if _state and _state._file:
Collin Winterce36ad82007-08-30 01:19:48 +000093 raise RuntimeError("input() already active")
Georg Brandlc98eeed2006-02-19 14:57:47 +000094 _state = FileInput(files, inplace, backup, bufsize, mode, openhook)
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000095 return _state
96
97def close():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000098 """Close the sequence."""
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000099 global _state
100 state = _state
101 _state = None
102 if state:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000103 state.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000104
105def nextfile():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000106 """
107 Close the current file so that the next iteration will read the first
108 line from the next file (if any); lines not read from the file will
109 not count towards the cumulative line count. The filename is not
110 changed until after the first line of the next file has been read.
111 Before the first line has been read, this function has no effect;
112 it cannot be used to skip the first file. After the last line of the
Tim Peters8ac14952002-05-23 15:15:30 +0000113 last file has been read, this function has no effect.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000114 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000115 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000116 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000117 return _state.nextfile()
118
119def filename():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000120 """
121 Return the name of the file currently being read.
Tim Peters8ac14952002-05-23 15:15:30 +0000122 Before the first line has been read, returns None.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000123 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000124 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000125 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000126 return _state.filename()
127
128def lineno():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000129 """
130 Return the cumulative line number of the line that has just been read.
131 Before the first line has been read, returns 0. After the last line
Tim Peters8ac14952002-05-23 15:15:30 +0000132 of the last file has been read, returns the line number of that line.
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.lineno()
137
138def filelineno():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000139 """
140 Return the line number in the current file. Before the first line
141 has been read, returns 0. After the last line of the last file has
Tim Peters8ac14952002-05-23 15:15:30 +0000142 been read, returns the line number of that line within the file.
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.filelineno()
147
Georg Brandl67e9fb92006-02-19 13:56:17 +0000148def fileno():
149 """
150 Return the file number of the current file. When no file is currently
151 opened, returns -1.
152 """
153 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000154 raise RuntimeError("no active input()")
Georg Brandl67e9fb92006-02-19 13:56:17 +0000155 return _state.fileno()
156
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000157def isfirstline():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000158 """
159 Returns true the line just read is the first line of its file,
Tim Peters8ac14952002-05-23 15:15:30 +0000160 otherwise returns false.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000161 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000162 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000163 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000164 return _state.isfirstline()
165
166def isstdin():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000167 """
168 Returns true if the last line was read from sys.stdin,
Tim Peters8ac14952002-05-23 15:15:30 +0000169 otherwise returns false.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000170 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000171 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000172 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000173 return _state.isstdin()
174
175class FileInput:
Terry Jan Reedy70d2c712013-06-28 18:59:28 -0400176 """FileInput([files[, inplace[, backup[, bufsize, [, mode[, openhook]]]]]])
Tim Peters8ac14952002-05-23 15:15:30 +0000177
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000178 Class FileInput is the implementation of the module; its methods
Georg Brandl67e9fb92006-02-19 13:56:17 +0000179 filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
180 nextfile() and close() correspond to the functions of the same name
181 in the module.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000182 In addition it has a readline() method which returns the next
183 input line, and a __getitem__() method which implements the
184 sequence behavior. The sequence must be accessed in strictly
Tim Peters8ac14952002-05-23 15:15:30 +0000185 sequential order; random access and readline() cannot be mixed.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000186 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000187
Georg Brandlef0a8652009-05-17 12:22:57 +0000188 def __init__(self, files=None, inplace=False, backup="", bufsize=0,
Georg Brandlc98eeed2006-02-19 14:57:47 +0000189 mode="r", openhook=None):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000190 if isinstance(files, str):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000191 files = (files,)
Roy Williams002665a2017-05-22 22:24:17 -0700192 elif isinstance(files, os.PathLike):
193 files = (os.fspath(files), )
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000194 else:
Guido van Rossum2516b392000-04-10 17:16:12 +0000195 if files is None:
196 files = sys.argv[1:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000197 if not files:
Guido van Rossum2516b392000-04-10 17:16:12 +0000198 files = ('-',)
199 else:
200 files = tuple(files)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000201 self._files = files
202 self._inplace = inplace
203 self._backup = backup
Serhiy Storchaka674e2d02016-03-08 18:35:19 +0200204 if bufsize:
205 import warnings
206 warnings.warn('bufsize is deprecated and ignored',
207 DeprecationWarning, stacklevel=2)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000208 self._savestdout = None
209 self._output = None
210 self._filename = None
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200211 self._startlineno = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000212 self._filelineno = 0
213 self._file = None
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000214 self._isstdin = False
Guido van Rossum0aec9fb1998-07-20 15:49:28 +0000215 self._backupfilename = None
Georg Brandlc029f872006-02-19 14:12:34 +0000216 # restrict mode argument to reading modes
217 if mode not in ('r', 'rU', 'U', 'rb'):
218 raise ValueError("FileInput opening mode must be one of "
219 "'r', 'rU', 'U' and 'rb'")
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200220 if 'U' in mode:
221 import warnings
Serhiy Storchaka2480c2e2013-11-24 23:13:26 +0200222 warnings.warn("'U' mode is deprecated",
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200223 DeprecationWarning, 2)
Georg Brandlc029f872006-02-19 14:12:34 +0000224 self._mode = mode
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200225 if openhook:
226 if inplace:
227 raise ValueError("FileInput cannot use an opening hook in inplace mode")
228 if not callable(openhook):
229 raise ValueError("FileInput openhook must be callable")
Georg Brandlc98eeed2006-02-19 14:57:47 +0000230 self._openhook = openhook
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000231
232 def __del__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000233 self.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000234
235 def close(self):
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300236 try:
237 self.nextfile()
238 finally:
239 self._files = ()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000240
Georg Brandl6cb7b652010-07-31 20:08:15 +0000241 def __enter__(self):
242 return self
243
244 def __exit__(self, type, value, traceback):
245 self.close()
246
Neil Schemenauer908632a2002-03-26 20:28:40 +0000247 def __iter__(self):
248 return self
249
Georg Brandla18af4e2007-04-21 15:47:16 +0000250 def __next__(self):
Serhiy Storchaka0554d832016-03-08 23:35:35 +0200251 while True:
252 line = self._readline()
253 if line:
254 self._filelineno += 1
255 return line
256 if not self._file:
257 raise StopIteration
258 self.nextfile()
259 # repeat with next file
Tim Peters863ac442002-04-16 01:38:40 +0000260
Neil Schemenauer908632a2002-03-26 20:28:40 +0000261 def __getitem__(self, i):
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200262 if i != self.lineno():
Collin Winterce36ad82007-08-30 01:19:48 +0000263 raise RuntimeError("accessing lines out of order")
Neil Schemenauer908632a2002-03-26 20:28:40 +0000264 try:
Georg Brandla18af4e2007-04-21 15:47:16 +0000265 return self.__next__()
Neil Schemenauer908632a2002-03-26 20:28:40 +0000266 except StopIteration:
Collin Winterce36ad82007-08-30 01:19:48 +0000267 raise IndexError("end of input reached")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000268
269 def nextfile(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000270 savestdout = self._savestdout
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300271 self._savestdout = None
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000272 if savestdout:
273 sys.stdout = savestdout
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000274
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000275 output = self._output
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300276 self._output = None
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300277 try:
278 if output:
279 output.close()
280 finally:
281 file = self._file
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300282 self._file = None
Serhiy Storchaka0554d832016-03-08 23:35:35 +0200283 try:
284 del self._readline # restore FileInput._readline
285 except AttributeError:
286 pass
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300287 try:
288 if file and not self._isstdin:
289 file.close()
290 finally:
291 backupfilename = self._backupfilename
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300292 self._backupfilename = None
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300293 if backupfilename and not self._backup:
294 try: os.unlink(backupfilename)
295 except OSError: pass
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000296
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300297 self._isstdin = False
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000298
299 def readline(self):
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200300 while True:
301 line = self._readline()
302 if line:
303 self._filelineno += 1
304 return line
305 if not self._file:
306 return line
307 self.nextfile()
308 # repeat with next file
309
Serhiy Storchaka0554d832016-03-08 23:35:35 +0200310 def _readline(self):
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200311 if not self._files:
312 if 'b' in self._mode:
313 return b''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000314 else:
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200315 return ''
316 self._filename = self._files[0]
317 self._files = self._files[1:]
318 self._startlineno = self.lineno()
319 self._filelineno = 0
320 self._file = None
321 self._isstdin = False
322 self._backupfilename = 0
323 if self._filename == '-':
324 self._filename = '<stdin>'
325 if 'b' in self._mode:
326 self._file = getattr(sys.stdin, 'buffer', sys.stdin)
327 else:
328 self._file = sys.stdin
329 self._isstdin = True
330 else:
331 if self._inplace:
332 self._backupfilename = (
Zhiming Wang06de1ae2017-09-05 01:37:24 +0800333 os.fspath(self._filename) + (self._backup or ".bak"))
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200334 try:
335 os.unlink(self._backupfilename)
336 except OSError:
337 pass
338 # The next few lines may raise OSError
339 os.rename(self._filename, self._backupfilename)
340 self._file = open(self._backupfilename, self._mode)
341 try:
342 perm = os.fstat(self._file.fileno()).st_mode
343 except OSError:
344 self._output = open(self._filename, "w")
345 else:
346 mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
347 if hasattr(os, 'O_BINARY'):
348 mode |= os.O_BINARY
349
350 fd = os.open(self._filename, mode, perm)
351 self._output = os.fdopen(fd, "w")
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200352 try:
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200353 if hasattr(os, 'chmod'):
354 os.chmod(self._filename, perm)
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200355 except OSError:
356 pass
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200357 self._savestdout = sys.stdout
358 sys.stdout = self._output
359 else:
360 # This may raise OSError
361 if self._openhook:
362 self._file = self._openhook(self._filename, self._mode)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000363 else:
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200364 self._file = open(self._filename, self._mode)
Serhiy Storchaka0554d832016-03-08 23:35:35 +0200365 self._readline = self._file.readline # hide FileInput._readline
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200366 return self._readline()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000367
368 def filename(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000369 return self._filename
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000370
371 def lineno(self):
Serhiy Storchakacc2dbc52016-03-08 18:28:36 +0200372 return self._startlineno + self._filelineno
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000373
374 def filelineno(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000375 return self._filelineno
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000376
Georg Brandl67e9fb92006-02-19 13:56:17 +0000377 def fileno(self):
378 if self._file:
379 try:
380 return self._file.fileno()
381 except ValueError:
382 return -1
383 else:
384 return -1
385
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000386 def isfirstline(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000387 return self._filelineno == 1
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000388
389 def isstdin(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000390 return self._isstdin
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000391
Georg Brandlc98eeed2006-02-19 14:57:47 +0000392
393def hook_compressed(filename, mode):
394 ext = os.path.splitext(filename)[1]
395 if ext == '.gz':
396 import gzip
397 return gzip.open(filename, mode)
398 elif ext == '.bz2':
399 import bz2
400 return bz2.BZ2File(filename, mode)
401 else:
402 return open(filename, mode)
403
404
Serhiy Storchakab2752102016-04-27 23:13:46 +0300405def hook_encoded(encoding, errors=None):
Georg Brandlc98eeed2006-02-19 14:57:47 +0000406 def openhook(filename, mode):
Serhiy Storchakab2752102016-04-27 23:13:46 +0300407 return open(filename, mode, encoding=encoding, errors=errors)
Georg Brandlc98eeed2006-02-19 14:57:47 +0000408 return openhook
409
410
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000411def _test():
412 import getopt
Georg Brandlef0a8652009-05-17 12:22:57 +0000413 inplace = False
414 backup = False
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000415 opts, args = getopt.getopt(sys.argv[1:], "ib:")
416 for o, a in opts:
Georg Brandlef0a8652009-05-17 12:22:57 +0000417 if o == '-i': inplace = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000418 if o == '-b': backup = a
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000419 for line in input(args, inplace=inplace, backup=backup):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000420 if line[-1:] == '\n': line = line[:-1]
421 if line[-1:] == '\r': line = line[:-1]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000422 print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
423 isfirstline() and "*" or "", line))
424 print("%d: %s[%d]" % (lineno(), filename(), filelineno()))
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000425
426if __name__ == '__main__':
427 _test()