禁止打印调用(python)

原文 : https://cloud.tencent.com/developer/ask/188486

 

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

 

posted @ 2019-11-16 20:45  cup_leo  阅读(793)  评论(0编辑  收藏  举报