blob: 3543653f268be48c87f3d7a70b3c0e38e93cbf3b [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",
Martin Panter7978e102016-01-16 06:26:54 +000085 "fileno", "isfirstline", "isstdin", "FileInput", "hook_compressed",
86 "hook_encoded"]
Skip Montanaroeccd02a2001-01-20 23:34:12 +000087
Guido van Rossum7d5b99d1997-11-21 17:12:59 +000088_state = None
89
Guido van Rossum47955242001-01-05 14:44:39 +000090DEFAULT_BUFSIZE = 8*1024
91
Georg Brandlef0a8652009-05-17 12:22:57 +000092def input(files=None, inplace=False, backup="", bufsize=0,
Georg Brandlc98eeed2006-02-19 14:57:47 +000093 mode="r", openhook=None):
Terry Jan Reedy70d2c712013-06-28 18:59:28 -040094 """Return an instance of the FileInput class, which can be iterated.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000095
Terry Jan Reedy70d2c712013-06-28 18:59:28 -040096 The parameters are passed to the constructor of the FileInput class.
97 The returned instance, in addition to being an iterator,
98 keeps global state for the functions of this module,.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +000099 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000100 global _state
101 if _state and _state._file:
Collin Winterce36ad82007-08-30 01:19:48 +0000102 raise RuntimeError("input() already active")
Georg Brandlc98eeed2006-02-19 14:57:47 +0000103 _state = FileInput(files, inplace, backup, bufsize, mode, openhook)
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000104 return _state
105
106def close():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000107 """Close the sequence."""
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000108 global _state
109 state = _state
110 _state = None
111 if state:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000112 state.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000113
114def nextfile():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000115 """
116 Close the current file so that the next iteration will read the first
117 line from the next file (if any); lines not read from the file will
118 not count towards the cumulative line count. The filename is not
119 changed until after the first line of the next file has been read.
120 Before the first line has been read, this function has no effect;
121 it cannot be used to skip the first file. After the last line of the
Tim Peters8ac14952002-05-23 15:15:30 +0000122 last file has been read, this function has no effect.
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.nextfile()
127
128def filename():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000129 """
130 Return the name of the file currently being read.
Tim Peters8ac14952002-05-23 15:15:30 +0000131 Before the first line has been read, returns None.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000132 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000133 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000134 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000135 return _state.filename()
136
137def lineno():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000138 """
139 Return the cumulative line number of the line that has just been read.
140 Before the first line has been read, returns 0. After the last line
Tim Peters8ac14952002-05-23 15:15:30 +0000141 of the last file has been read, returns the line number of that line.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000142 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000143 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000144 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000145 return _state.lineno()
146
147def filelineno():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000148 """
149 Return the line number in the current file. Before the first line
150 has been read, returns 0. After the last line of the last file has
Tim Peters8ac14952002-05-23 15:15:30 +0000151 been read, returns the line number of that line within the file.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000152 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000153 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000154 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000155 return _state.filelineno()
156
Georg Brandl67e9fb92006-02-19 13:56:17 +0000157def fileno():
158 """
159 Return the file number of the current file. When no file is currently
160 opened, returns -1.
161 """
162 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000163 raise RuntimeError("no active input()")
Georg Brandl67e9fb92006-02-19 13:56:17 +0000164 return _state.fileno()
165
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000166def isfirstline():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000167 """
168 Returns true the line just read is the first line of its file,
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.isfirstline()
174
175def isstdin():
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000176 """
177 Returns true if the last line was read from sys.stdin,
Tim Peters8ac14952002-05-23 15:15:30 +0000178 otherwise returns false.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000179 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000180 if not _state:
Collin Winterce36ad82007-08-30 01:19:48 +0000181 raise RuntimeError("no active input()")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000182 return _state.isstdin()
183
184class FileInput:
Terry Jan Reedy70d2c712013-06-28 18:59:28 -0400185 """FileInput([files[, inplace[, backup[, bufsize, [, mode[, openhook]]]]]])
Tim Peters8ac14952002-05-23 15:15:30 +0000186
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000187 Class FileInput is the implementation of the module; its methods
Georg Brandl67e9fb92006-02-19 13:56:17 +0000188 filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
189 nextfile() and close() correspond to the functions of the same name
190 in the module.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000191 In addition it has a readline() method which returns the next
192 input line, and a __getitem__() method which implements the
193 sequence behavior. The sequence must be accessed in strictly
Tim Peters8ac14952002-05-23 15:15:30 +0000194 sequential order; random access and readline() cannot be mixed.
Raymond Hettingerd1fa3db2002-05-15 02:56:03 +0000195 """
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000196
Georg Brandlef0a8652009-05-17 12:22:57 +0000197 def __init__(self, files=None, inplace=False, backup="", bufsize=0,
Georg Brandlc98eeed2006-02-19 14:57:47 +0000198 mode="r", openhook=None):
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000199 if isinstance(files, str):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000200 files = (files,)
201 else:
Guido van Rossum2516b392000-04-10 17:16:12 +0000202 if files is None:
203 files = sys.argv[1:]
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000204 if not files:
Guido van Rossum2516b392000-04-10 17:16:12 +0000205 files = ('-',)
206 else:
207 files = tuple(files)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000208 self._files = files
209 self._inplace = inplace
210 self._backup = backup
Guido van Rossum47955242001-01-05 14:44:39 +0000211 self._bufsize = bufsize or DEFAULT_BUFSIZE
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000212 self._savestdout = None
213 self._output = None
214 self._filename = None
215 self._lineno = 0
216 self._filelineno = 0
217 self._file = None
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000218 self._isstdin = False
Guido van Rossum0aec9fb1998-07-20 15:49:28 +0000219 self._backupfilename = None
Guido van Rossum47955242001-01-05 14:44:39 +0000220 self._buffer = []
221 self._bufindex = 0
Georg Brandlc029f872006-02-19 14:12:34 +0000222 # restrict mode argument to reading modes
223 if mode not in ('r', 'rU', 'U', 'rb'):
224 raise ValueError("FileInput opening mode must be one of "
225 "'r', 'rU', 'U' and 'rb'")
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200226 if 'U' in mode:
227 import warnings
Serhiy Storchaka2480c2e2013-11-24 23:13:26 +0200228 warnings.warn("'U' mode is deprecated",
Serhiy Storchaka6787a382013-11-23 22:12:06 +0200229 DeprecationWarning, 2)
Georg Brandlc029f872006-02-19 14:12:34 +0000230 self._mode = mode
Florent Xicluna5d1155c2011-10-28 14:45:05 +0200231 if openhook:
232 if inplace:
233 raise ValueError("FileInput cannot use an opening hook in inplace mode")
234 if not callable(openhook):
235 raise ValueError("FileInput openhook must be callable")
Georg Brandlc98eeed2006-02-19 14:57:47 +0000236 self._openhook = openhook
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000237
238 def __del__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000239 self.close()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000240
241 def close(self):
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300242 try:
243 self.nextfile()
244 finally:
245 self._files = ()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000246
Georg Brandl6cb7b652010-07-31 20:08:15 +0000247 def __enter__(self):
248 return self
249
250 def __exit__(self, type, value, traceback):
251 self.close()
252
Neil Schemenauer908632a2002-03-26 20:28:40 +0000253 def __iter__(self):
254 return self
255
Georg Brandla18af4e2007-04-21 15:47:16 +0000256 def __next__(self):
Guido van Rossum47955242001-01-05 14:44:39 +0000257 try:
258 line = self._buffer[self._bufindex]
259 except IndexError:
260 pass
261 else:
262 self._bufindex += 1
263 self._lineno += 1
264 self._filelineno += 1
265 return line
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000266 line = self.readline()
267 if not line:
Neil Schemenauer908632a2002-03-26 20:28:40 +0000268 raise StopIteration
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000269 return line
Tim Peters863ac442002-04-16 01:38:40 +0000270
Neil Schemenauer908632a2002-03-26 20:28:40 +0000271 def __getitem__(self, i):
272 if i != self._lineno:
Collin Winterce36ad82007-08-30 01:19:48 +0000273 raise RuntimeError("accessing lines out of order")
Neil Schemenauer908632a2002-03-26 20:28:40 +0000274 try:
Georg Brandla18af4e2007-04-21 15:47:16 +0000275 return self.__next__()
Neil Schemenauer908632a2002-03-26 20:28:40 +0000276 except StopIteration:
Collin Winterce36ad82007-08-30 01:19:48 +0000277 raise IndexError("end of input reached")
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000278
279 def nextfile(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000280 savestdout = self._savestdout
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300281 self._savestdout = None
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000282 if savestdout:
283 sys.stdout = savestdout
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000284
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000285 output = self._output
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300286 self._output = None
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300287 try:
288 if output:
289 output.close()
290 finally:
291 file = self._file
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300292 self._file = None
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300293 try:
294 if file and not self._isstdin:
295 file.close()
296 finally:
297 backupfilename = self._backupfilename
Serhiy Storchaka2116b122015-04-10 13:29:28 +0300298 self._backupfilename = None
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300299 if backupfilename and not self._backup:
300 try: os.unlink(backupfilename)
301 except OSError: pass
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000302
Serhiy Storchaka7e7a3db2015-04-10 13:24:41 +0300303 self._isstdin = False
304 self._buffer = []
305 self._bufindex = 0
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000306
307 def readline(self):
Guido van Rossum47955242001-01-05 14:44:39 +0000308 try:
309 line = self._buffer[self._bufindex]
310 except IndexError:
311 pass
312 else:
313 self._bufindex += 1
314 self._lineno += 1
315 self._filelineno += 1
316 return line
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000317 if not self._file:
318 if not self._files:
Serhiy Storchaka314464d2015-11-01 16:43:58 +0200319 if 'b' in self._mode:
320 return b''
321 else:
322 return ''
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000323 self._filename = self._files[0]
324 self._files = self._files[1:]
325 self._filelineno = 0
326 self._file = None
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000327 self._isstdin = False
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000328 self._backupfilename = 0
329 if self._filename == '-':
330 self._filename = '<stdin>'
Serhiy Storchaka946cfc32014-05-14 21:08:33 +0300331 if 'b' in self._mode:
R David Murray830207e2016-01-02 15:41:41 -0500332 self._file = getattr(sys.stdin, 'buffer', sys.stdin)
Serhiy Storchaka946cfc32014-05-14 21:08:33 +0300333 else:
334 self._file = sys.stdin
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000335 self._isstdin = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000336 else:
337 if self._inplace:
338 self._backupfilename = (
Skip Montanaro7a98be22007-08-16 14:35:24 +0000339 self._filename + (self._backup or ".bak"))
Andrew Svetlovad28c7f2012-12-18 22:02:39 +0200340 try:
341 os.unlink(self._backupfilename)
342 except OSError:
343 pass
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200344 # The next few lines may raise OSError
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000345 os.rename(self._filename, self._backupfilename)
Georg Brandlc029f872006-02-19 14:12:34 +0000346 self._file = open(self._backupfilename, self._mode)
Guido van Rossumdcb85831999-10-18 21:41:43 +0000347 try:
Walter Dörwald294bbf32002-06-06 09:48:13 +0000348 perm = os.fstat(self._file.fileno()).st_mode
Skip Montanarocffac662002-08-14 02:58:16 +0000349 except OSError:
Guido van Rossumdcb85831999-10-18 21:41:43 +0000350 self._output = open(self._filename, "w")
351 else:
Guido van Rossum6203d8f2007-10-29 17:39:59 +0000352 mode = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
353 if hasattr(os, 'O_BINARY'):
354 mode |= os.O_BINARY
355
356 fd = os.open(self._filename, mode, perm)
Guido van Rossumdcb85831999-10-18 21:41:43 +0000357 self._output = os.fdopen(fd, "w")
358 try:
Jack Jansen52941a82003-01-08 16:33:16 +0000359 if hasattr(os, 'chmod'):
360 os.chmod(self._filename, perm)
Skip Montanarocffac662002-08-14 02:58:16 +0000361 except OSError:
Guido van Rossumdcb85831999-10-18 21:41:43 +0000362 pass
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000363 self._savestdout = sys.stdout
364 sys.stdout = self._output
365 else:
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200366 # This may raise OSError
Georg Brandlc98eeed2006-02-19 14:57:47 +0000367 if self._openhook:
368 self._file = self._openhook(self._filename, self._mode)
369 else:
370 self._file = open(self._filename, self._mode)
Guido van Rossum47955242001-01-05 14:44:39 +0000371 self._buffer = self._file.readlines(self._bufsize)
372 self._bufindex = 0
373 if not self._buffer:
374 self.nextfile()
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000375 # Recursive call
376 return self.readline()
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000377
378 def filename(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000379 return self._filename
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000380
381 def lineno(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000382 return self._lineno
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000383
384 def filelineno(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000385 return self._filelineno
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000386
Georg Brandl67e9fb92006-02-19 13:56:17 +0000387 def fileno(self):
388 if self._file:
389 try:
390 return self._file.fileno()
391 except ValueError:
392 return -1
393 else:
394 return -1
395
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000396 def isfirstline(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000397 return self._filelineno == 1
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000398
399 def isstdin(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000400 return self._isstdin
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000401
Georg Brandlc98eeed2006-02-19 14:57:47 +0000402
403def hook_compressed(filename, mode):
404 ext = os.path.splitext(filename)[1]
405 if ext == '.gz':
406 import gzip
407 return gzip.open(filename, mode)
408 elif ext == '.bz2':
409 import bz2
410 return bz2.BZ2File(filename, mode)
411 else:
412 return open(filename, mode)
413
414
415def hook_encoded(encoding):
Georg Brandlc98eeed2006-02-19 14:57:47 +0000416 def openhook(filename, mode):
Florent Xiclunaa011e2b2011-11-07 19:43:07 +0100417 return open(filename, mode, encoding=encoding)
Georg Brandlc98eeed2006-02-19 14:57:47 +0000418 return openhook
419
420
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000421def _test():
422 import getopt
Georg Brandlef0a8652009-05-17 12:22:57 +0000423 inplace = False
424 backup = False
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000425 opts, args = getopt.getopt(sys.argv[1:], "ib:")
426 for o, a in opts:
Georg Brandlef0a8652009-05-17 12:22:57 +0000427 if o == '-i': inplace = True
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000428 if o == '-b': backup = a
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000429 for line in input(args, inplace=inplace, backup=backup):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000430 if line[-1:] == '\n': line = line[:-1]
431 if line[-1:] == '\r': line = line[:-1]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000432 print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
433 isfirstline() and "*" or "", line))
434 print("%d: %s[%d]" % (lineno(), filename(), filelineno()))
Guido van Rossum7d5b99d1997-11-21 17:12:59 +0000435
436if __name__ == '__main__':
437 _test()