class StdoutFilter(TextIOWrapper):

    def __init__(self, stdout):
        super().__init__(stdout)
self.stdout = stdout

def write(self, output):
    if output != "don't write this":
        self.stdout.write(output)
sys.stdout = StdoutFilter(sys.stdout)
print('hello, world!')
print("don't write this")
sys.stdout = sys.__stdout__
Prevent string being printed python