I understand why I am getting the "Bad file descriptor" error when printing with no console from this post: why am I getting IOError: (9, 'Bad file descriptor') error while making print statements?.
My question is, how can I detect if stdout is available? Can I simply do something like this:
if os.path.isfile(2):
print "text"
Thanks
os.path.isfile() takes a file path (a string), not a file descriptor (a number), so your solution will not work as you expect.
You can use os.isatty() instead:
if os.isatty(1):
print "text"
os.isatty() will return True if its argument is an open file descriptor connected to a terminal.
(In passing, note that stdout is file descriptor 1. stderr is file descriptor 2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With