2828read, filename() and the line number functions return the values
2929pertaining to the last line read; nextfile() has no effect.
3030
31- All files are opened in text mode. If an I/O error occurs during
32- opening or reading a file, the IOError exception is raised.
31+ All files are opened in text mode by default, you can override this by
32+ setting the mode parameter to input() or FileInput.__init__().
33+ If an I/O error occurs during opening or reading a file, the IOError
34+ exception is raised.
3335
3436If sys.stdin is used more than once, the second and further use will
3537return no lines, except perhaps for interactive use, or if it has been
7274XXX Possible additions:
7375
7476- optional getopt argument processing
75- - specify open mode ('r' or 'rb')
7677- isatty()
7778- read(), read(size), even readlines()
7879
8788
8889DEFAULT_BUFSIZE = 8 * 1024
8990
90- def input (files = None , inplace = 0 , backup = "" , bufsize = 0 ):
91- """input([files[, inplace[, backup]]])
91+ def input (files = None , inplace = 0 , backup = "" , bufsize = 0 , mode = "r" ):
92+ """input([files[, inplace[, backup[, mode] ]]])
9293
9394 Create an instance of the FileInput class. The instance will be used
9495 as global state for the functions of this module, and is also returned
@@ -98,7 +99,7 @@ def input(files=None, inplace=0, backup="", bufsize=0):
9899 global _state
99100 if _state and _state ._file :
100101 raise RuntimeError , "input() already active"
101- _state = FileInput (files , inplace , backup , bufsize )
102+ _state = FileInput (files , inplace , backup , bufsize , mode )
102103 return _state
103104
104105def close ():
@@ -180,7 +181,7 @@ def isstdin():
180181 return _state .isstdin ()
181182
182183class FileInput :
183- """class FileInput([files[, inplace[, backup]]])
184+ """class FileInput([files[, inplace[, backup[, mode] ]]])
184185
185186 Class FileInput is the implementation of the module; its methods
186187 filename(), lineno(), fileline(), isfirstline(), isstdin(), fileno(),
@@ -192,7 +193,7 @@ class FileInput:
192193 sequential order; random access and readline() cannot be mixed.
193194 """
194195
195- def __init__ (self , files = None , inplace = 0 , backup = "" , bufsize = 0 ):
196+ def __init__ (self , files = None , inplace = 0 , backup = "" , bufsize = 0 , mode = "r" ):
196197 if isinstance (files , basestring ):
197198 files = (files ,)
198199 else :
@@ -216,6 +217,11 @@ def __init__(self, files=None, inplace=0, backup="", bufsize=0):
216217 self ._backupfilename = None
217218 self ._buffer = []
218219 self ._bufindex = 0
220+ # restrict mode argument to reading modes
221+ if mode not in ('r' , 'rU' , 'U' , 'rb' ):
222+ raise ValueError ("FileInput opening mode must be one of "
223+ "'r', 'rU', 'U' and 'rb'" )
224+ self ._mode = mode
219225
220226 def __del__ (self ):
221227 self .close ()
@@ -307,7 +313,7 @@ def readline(self):
307313 except os .error : pass
308314 # The next few lines may raise IOError
309315 os .rename (self ._filename , self ._backupfilename )
310- self ._file = open (self ._backupfilename , "r" )
316+ self ._file = open (self ._backupfilename , self . _mode )
311317 try :
312318 perm = os .fstat (self ._file .fileno ()).st_mode
313319 except OSError :
@@ -326,7 +332,7 @@ def readline(self):
326332 sys .stdout = self ._output
327333 else :
328334 # This may raise IOError
329- self ._file = open (self ._filename , "r" )
335+ self ._file = open (self ._filename , self . _mode )
330336 self ._buffer = self ._file .readlines (self ._bufsize )
331337 self ._bufindex = 0
332338 if not self ._buffer :
0 commit comments