redirect output of ipython script into a csv or text file like sqlplus spool
<p>I try to redirect the output of my script to a file.
I don't want to do something like</p>

<pre><code>python myscript.py &gt; xy.out
</code></pre>

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

<p>I try to follow this link </p>

<p><a href="https://stackoverflow.com/questions/14571090/ipython-redirecting-output-of-a-python-script-to-a-file-like-bash">IPython: redirecting output of a Python script to a file (like bash &gt;)</a></p>

<p>however, when I try to do </p>

<pre><code>with redirect_output("my_output.txt"):
    %run my_script.py
</code></pre>

<p>It gives the error </p>

<pre><code>---&gt; 10         self.sys_stdout = sys.stdout
NameError: global name 'sys' is not defined
</code></pre>

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

<p><a href="https://www.quora.com/How-do-I-export-the-output-of-the-IPython-command-to-a-text-file-or-a-CSV-file" rel="nofollow noreferrer">https://www.quora.com/How-do-I-export-the-output-of-the-IPython-command-to-a-text-file-or-a-CSV-file</a></p>

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

<p>In oracle sqlplus there is a spool command 
e.g.</p>

<pre><code>spool /tmp/abc
select * from table_x;
spool off
</code></pre>

<p>now the sql statement output is in /tmp/abc</p>

<p>Is such a equivalent for ipython ??</p>