python - print: "IOError: [Errno 9] Bad file descriptor" -


i understand why getting "bad file descriptor" error when printing no console post: why getting ioerror: (9, 'bad file descriptor') error while making print statements?.

my question is, how can detect if stdout available? can this:

if os.path.isfile(2):    print "text" 

thanks

os.path.isfile() takes file path (a string), not file descriptor (a number), solution not work expect.

you can use os.isatty() instead:

if os.isatty(1):     print "text" 

os.isatty() return true if argument open file descriptor connected terminal.

(in passing, note stdout file descriptor 1. stderr file descriptor 2).


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -