Skip to content Skip to sidebar Skip to footer

Redirect Output Of Ipython Script Into A Csv Or Text File Like Sqlplus Spool

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 enviro

Solution 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!

Solution 2:

import time
from threading import Thread
import sys
#write the stdout to filedeflog():
    #for stop the threadglobal run
    while (run):
        try:
            global out
            text = str(sys.stdout.getvalue())
            withopen("out.txt", 'w') as f:
                f.write(text)
        finally:
            time.sleep(1)
%%capture out
run = Trueprint("start")
process = Thread(target=log, args=[]).start()

# do some workfor i inrange(10, 1000):
    print(i)
    time.sleep(1)
run= False
process.join()

Post a Comment for "Redirect Output Of Ipython Script Into A Csv Or Text File Like Sqlplus Spool"