I am running a python2.5 script on a windows 2003 server as a service. I am getting this error for simple print statments:
IOError: (9, 'Bad file descriptor')
I deleted all the print statements because they were only used for development purposes, but I am unsure why a print statement would cause me any greif. I ran the same script not as a service without any major problems. Just wondering if anyone else has any insight?
In Python 2.x, this is the expected behavior. In this bug report, Christian Heimes explains that it is a design decision:
He also recommends a workaround for obtaining Python 3.x-style
print()
behavior in Python 2.7:You can't print because
sys.stdout
is not available when not running as a console session.Instead of using
print
statements you can consider using thelogging
module so you can set the loglevel and write all critical things to the system event log.It should be noted that you can still get it to work (or silently ignore the problem) by doing something like this:
To write to a file per output stream:
To write to a single file:
Or to silently ignore all print statements: