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 ??

1
  • Did you import sys, before trying the redirect_output function? Commented Jun 17, 2018 at 3:01

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

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...
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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.