Allow setting mode on opening of file

svn: r17520
This commit is contained in:
Michiel Nauta 2011-05-17 17:22:41 +00:00
parent d324edfcfb
commit d85246d5d1

View File

@ -319,16 +319,16 @@ class OpenFileOrStdout:
#-------------------------------------------------------------------------
class OpenFileOrStdin:
"""Context manager to open a file or stdin for reading."""
def __init__(self, filename):
def __init__(self, filename, add_mode=''):
self.filename = filename
self.mode = 'r%s' % add_mode
self.filehandle = None
def __enter__(self):
if self.filename == '-':
# TODO how to add U to mode?
self.filehandle = sys.stdin
else:
self.filehandle = open(self.filename, "rU")
self.filehandle = open(self.filename, self.mode)
return self.filehandle
def __exit__(self, exc_type, exc_value, traceback):