python turn off printing

Solutions on MaxInterview for python turn off printing by the best coders in the world

showing results for - "python turn off printing"
Anton
13 Aug 2018
1import os, sys
2
3class HiddenPrints:
4    def __enter__(self):
5        self._original_stdout = sys.stdout
6        sys.stdout = open(os.devnull, 'w')
7
8    def __exit__(self, exc_type, exc_val, exc_tb):
9        sys.stdout.close()
10        sys.stdout = self._original_stdout
11
12with HiddenPrints():
13  print("This wont print")