📜  python 将控制台输出重定向到 devnull - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:46.056000             🧑  作者: Mango

代码示例1
from contextlib import contextmanager
import sys, os

@contextmanager
def suppress_stdout():
    with open(os.devnull, "w") as devnull:
        old_stdout = sys.stdout
        sys.stdout = devnull
        try:  
            yield
        finally:
            sys.stdout = old_stdout