1# credit to user in Stack Overflow
2
3import os, sys
4
5class HiddenPrints:
6 def __enter__(self):
7 self._original_stdout = sys.stdout
8 sys.stdout = open(os.devnull, 'w')
9
10 def __exit__(self, exc_type, exc_val, exc_tb):
11 sys.stdout.close()
12 sys.stdout = self._original_stdout
13
14with HiddenPrints():
15 print('Will not be printed')
16
17print('Will be printed')