0

I have the exact same question as Python + Disabled print output, and can't get it back. I used the following code to suppress printing:

# Disable
def blockPrint():
sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
sys.stdout = sys.__stdout__

The top solution to this question was to store the old stdin by the following line, but I have already disabled printing. How can I restore this?

sys.__stdout__ = sys.stdout
martineau
  • 119,623
  • 25
  • 170
  • 301
user3737057
  • 121
  • 3
  • 12

1 Answers1

1

To summarize the comments above:

  • Disabling it only lasts during the program run. Closing and restarting it will fix it.
  • To disable and restore in the same program run, store sys.stdin in a temporary variable before disabling it, like this:
temp_stdout = None

# Disable
def blockPrint():
    global temp_stdout
    temp_stdout = sys.stdout
    sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
    global temp_stdout
    sys.stdout = temp_stdout
Alex Mandelias
  • 436
  • 5
  • 10