I would like the following basic construct:
if fname:
fhandle=open(fname,"w")
else:
fhandle=sys.stdout
...code using fhandle for output....
Normally, I would open the file using "with" like this:
with open(name,"w") as fhandle:
.... code using handle for output....
Is there a way to blend these two constructs, so that I can pass something to the open function in the with construct that will make fhandle point to sys.stdout? Or, if this is a dumb idea, what's the pythonic way to do this?