I want a function returning a file object with which I can write binary data to standard output. In python2 sys.stdout
is such an object. In python3 it is sys.stdout.buffer
.
What is the most elegant/preferred way to retrieve such an object so that it works for both, the python2 and the python3 interpreter?
Is the best way to check for existance of sys.stdout.buffer
(probably using the inspect
module) and if it exists, return it and if it doesnt, assume we are on python2 and return sys.stdout
instead?