ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 23 Oct 2010 07:01:35 +0200How to output from sagehttps://ask.sagemath.org/question/7738/how-to-output-from-sage/If I am in a sage shell, and I want to send the output of my sage program(a .py file in this case) to a text file, how can I do this?
I would rather use a command in the shell, than write it into my python script.Fri, 22 Oct 2010 22:19:35 +0200https://ask.sagemath.org/question/7738/how-to-output-from-sage/Answer by ccanonc for <p>If I am in a sage shell, and I want to send the output of my sage program(a .py file in this case) to a text file, how can I do this?</p>
<p>I would rather use a command in the shell, than write it into my python script.</p>
https://ask.sagemath.org/question/7738/how-to-output-from-sage/?answer=11648#post-id-11648<pre>
with open("outputfilepath", 'w') as fp:
for line in iterable: #assuming line is str with \n
print >> fp, line
#implicit fp.close() here as "with" scope ends.
</pre>Sat, 23 Oct 2010 01:51:27 +0200https://ask.sagemath.org/question/7738/how-to-output-from-sage/?answer=11648#post-id-11648Answer by mhampton for <p>If I am in a sage shell, and I want to send the output of my sage program(a .py file in this case) to a text file, how can I do this?</p>
<p>I would rather use a command in the shell, than write it into my python script.</p>
https://ask.sagemath.org/question/7738/how-to-output-from-sage/?answer=11626#post-id-11626It really depends what your output is, but for some things you could do:
sage: f = file('/desired/path/output.txt','w')
sage: f.write(str(my_amazing_output))
sage: f.close()
Maybe I don't understand the question though.
Sat, 23 Oct 2010 01:12:06 +0200https://ask.sagemath.org/question/7738/how-to-output-from-sage/?answer=11626#post-id-11626Answer by Mitesh Patel for <p>If I am in a sage shell, and I want to send the output of my sage program(a .py file in this case) to a text file, how can I do this?</p>
<p>I would rather use a command in the shell, than write it into my python script.</p>
https://ask.sagemath.org/question/7738/how-to-output-from-sage/?answer=11750#post-id-11750It's possible to redirect `sys.stdout` and `sys.stderr` temporarily to files. See [Example 10.9. Redirecting output][1] from Mark Pilgrim's [Dive into Python][2].
Note: Redirecting stdout will also send the Sage prompt `sage: ` to the output file. You can suppress most of the prompt with
`sage.misc.interpreter.set_sage_prompt('')`. I don't know if it's also possible to remove the colon that IPython inserts.
And also by the way, you might find IPython's "magic" `%logstart` and `%logstop` commands somewhat useful. See `%logstart?` for options.
[1]: http://diveintopython.org/scripts_and_streams/stdin_stdout_stderr.html
[2]: http://diveintopython.org/index.htmlSat, 23 Oct 2010 07:01:35 +0200https://ask.sagemath.org/question/7738/how-to-output-from-sage/?answer=11750#post-id-11750