I don't think you need to use a context manager in this case because there are no other operations between the opening and closing of the file.
Anyway I wouldn't do it like that.
Keep in mind that if you use sys.stdout this way you will close it which means that any attempt to write on it after your with statement will result in an error:
I'm just trying to keep the code clear and non-repetitive. In terms of level of effort and maintainability, if it needs to be able to optionally write to a file, breaking the inner logic out into a function as inspired by @thinkslynk
is probably best.
But if it's a one-off project it might be even better to only write to stdout and pipe it to a file in the shell if needed. (Sometimes it's fun to over-engineer though.)
I think the simplest is the first one :D
Or you can wrap that if in a normal function if you plan to use it more than once:
I don't think you need to use a context manager in this case because there are no other operations between the opening and closing of the file.
Anyway I wouldn't do it like that.
Keep in mind that if you use sys.stdout this way you will close it which means that any attempt to write on it after your
with
statement will result in an error:So why do you need to hide stdout this way? It's probably easier to use StringIO, write to it and then use a print to display it on stdout.
I'm just trying to keep the code clear and non-repetitive. In terms of level of effort and maintainability, if it needs to be able to optionally write to a file, breaking the inner logic out into a function as inspired by @thinkslynk is probably best.
But if it's a one-off project it might be even better to only write to stdout and pipe it to a file in the shell if needed. (Sometimes it's fun to over-engineer though.)
Actually the context manager solves this by only opening/closing the fp if it isn't to stdout so it might be the best solution
Oh yes, you're right :-)
+1 to the context manager, though it might be overkill if the function is used only once.