why am I getting IOError: (9, 'Bad file descriptor') error while making print statements?

后端 未结 2 407
生来不讨喜
生来不讨喜 2020-12-30 02:33

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         


        
2条回答
  •  遥遥无期
    2020-12-30 02:48

    In Python 2.x, this is the expected behavior. In this bug report, Christian Heimes explains that it is a design decision:

    I recommend against changing the code so late in the Python 2.7 release cycle. A change in behavior is too confusing. And it's not a bug but a design decision, too. Over five years ago I implement parts of the IO interaction with the operating system for Python 3.0. I deliberately did NOT port modifications to 2.6.

    He also recommends a workaround for obtaining Python 3.x-style print() behavior in Python 2.7:

    from __future__ import print_function
    import sys
    if sys.executable.endswith("pythonw.exe"):
        sys.stdout = sys.stdout = None
    
    print("can handle sys.stdout = None just fine.")
    

提交回复
热议问题