相当于Python 2中的Python 3 sys.stdout.fileno()

问题描述 投票:1回答:1

我正在使用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做到这一点?

python python-2.7 stdout sys
1个回答
0
投票

问题是sys.stdout被FlushingStringIO替换为一个库:https://github.com/flexible-atomic-code/fac。它也以某种方式取决于此细节,否则会中断。由于该库没有合理的替代方法,因此在这种情况下无法抑制stdout输出。

© www.soinside.com 2019 - 2024. All rights reserved.