📜  python 关闭打印 - Python 代码示例

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

代码示例1
import os, sys

class HiddenPrints:
    def __enter__(self):
        self._original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout.close()
        sys.stdout = self._original_stdout

with HiddenPrints():
  print("This wont print")