我正在使用Python 2.7编写代码。我使用一个非常具体的库,该库使用Python API部分用C编写。该库使用printf输出到stdout。我想抑制这一点。我已经找到了这个问题:How do I prevent a C shared library to print on stdout in python?所有提供的答案都使用sys.stdout.fileno()。当我运行代码时,我得到:
original_stdout_fd = sys.stdout.fileno()
AttributeError: 'FlushingStringIO' object has no attribute 'fileno'
我怀疑问题是我正在使用Python 2.7。我的假设是否正确,是否有办法使用Python 2.7做到这一点?
问题是sys.stdout被FlushingStringIO替换为一个库:https://github.com/flexible-atomic-code/fac。它也以某种方式取决于此细节,否则会中断。由于该库没有合理的替代方法,因此在这种情况下无法抑制stdout输出。