0

I try to redirect the output of my script to a file. I don't want to do something like

python myscript.py > xy.out

as a lot of the variable is being stored in my ipython environment and I like to carry it over.

I try to follow this link

IPython: redirecting output of a Python script to a file (like bash >)

however, when I try to do

with redirect_output("my_output.txt"):
    %run my_script.py

It gives the error

---> 10         self.sys_stdout = sys.stdout
NameError: global name 'sys' is not defined

There is a similar solution to copy the output of a ipython shell to a file but It says my cell its not defined

https://www.quora.com/How-do-I-export-the-output-of-the-IPython-command-to-a-text-file-or-a-CSV-file

Is there a easier way or build in feature with ipython that does that ?? e.g.

In oracle sqlplus there is a spool command e.g.

spool /tmp/abc
select * from table_x;
spool off

now the sql statement output is in /tmp/abc

Is such a equivalent for ipython ??

Community
  • 1
  • 1
Mookayama
  • 1,243
  • 2
  • 14
  • 28

2 Answers2

1

Greeting. I end up using this solution:

%%capture var2
%run -I script2

import sys
orig_stdout = sys.stdout
f = file('out5.txt', 'w')
sys.stdout = f 
print var2
stdout = orig_stdout
f.close()

It is not very handy but it works!

Mookayama
  • 1,243
  • 2
  • 14
  • 28
  • From what I understand, this will wait till the end of your code, and then dump it into the file. Is that what you were expecting? The advantage of the `redirect_output` function, as far as I can see is that it will write to the file as the output is generated.. so you can see the output in the log file in real time... – alpha_989 Jun 17 '18 at 02:59
1
import time
from threading import Thread
import sys
#write the stdout to file
def log():
    #for stop the thread
    global run
    while (run):
        try:
            global out
            text = str(sys.stdout.getvalue())
            with open("out.txt", 'w') as f:
                f.write(text)
        finally:
            time.sleep(1)
%%capture out
run = True
print("start")
process = Thread(target=log, args=[]).start()

# do some work
for i in range(10, 1000):
    print(i)
    time.sleep(1)
run= False
process.join()
Raful Chizkiyahu
  • 364
  • 2
  • 15