0

How do I set a Jupyter notebook's output back to the output cells after sending its output to a file?

Sending its output to a file:

sys.stdout = open(filename, 'w') print "stuff" 

Sending its output back to the output cells:

sys.stdout = ? print "hopefully this is in an output cell now" 
1
  • 1
    Possible duplicate of Redirect stdout to a file in Python? The second answer is much better than the accepted one, as it ensures proper cleanup and avoids having to write a bunch of manual code. Commented Sep 25, 2019 at 13:42

1 Answer 1

4

Use a temporary variable:

stdout_backup = sys.stdout 

Sending its output to a file:

sys.stdout = open(filename, 'w') print "stuff" 

Sending its output back to the output cells:

sys.stdout = stdout_backup print "this is in an output cell now" 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.