Issue
Inside a jupyter notebook based on ipython I have a function that prints something. I can not change this function as it is from a library. Let's take
def print_something():
print("something")
as an example. When I run print_something()
in a cell, I want to save whatever the cell is printing in a file. How do I do that? I tried %save
and %logstart
, but as print
is not creating an output, they don't do what I want.
Solution
Thanks to the comments I have found the answer in another Question:
%%capture cap --no-stderr
print_something()
with open('/path/to/capture.txt') as f:
f.write(cap.stdout)
Documentation: https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-capture
Answered By - Niclas von Caprivi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.