Here is a piece of code that I just tried in iPython notebook v2.3 that seems to do what was requested:
import sys
import IPython.utils.io
outputstream = IPython.utils.io.Tee("outputfile.log", "w", channel="stdout")
outputstream.write("Hello worlds!\n")
outputstream.close()
logstream=open("outputfile.log", "r")
sys.stdout.write("Read back from log file:\n")
sys.stdout.write(logstream.read())
The log file is created in the same directory as the iPython laptop file, and the output from this cell is displayed as follows:
Hello worlds!
Read back from log file:
Hello worlds!
I have not tried this in the iPython terminal, but I see no reason why it will not work.
(Researched and answered as part of Oxford's participation at http://aaronswartzhackathon.org )
source
share