Answers for "python prevent print output"

0

python turn off printing

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")
Posted by: Guest on March-31-2020
0

python prevent print output

import io
import sys
def salute(name): 
  """Says hi to someone."""    
  print('Hi, {}!'.format(name))
# create a text trap and redirect 
stdouttext_trap = io.StringIO()
sys.stdout = text_trap
# execute our now mute function
salute('Anne')
# now restore 
stdout function
sys.stdout = sys.__stdout__
Posted by: Guest on September-17-2020

Python Answers by Framework

Browse Popular Code Answers by Language