Ask Your Question
6

How to output from sage

asked 2010-10-22 15:19:35 -0500

schof gravatar image

updated 2010-10-22 15:20:17 -0500

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.

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
5

answered 2010-10-22 18:12:06 -0500

mhampton gravatar image

It 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.

edit flag offensive delete link more
2

answered 2010-10-23 00:01:35 -0500

Mitesh Patel gravatar image

It's possible to redirect sys.stdout and sys.stderr temporarily to files. See Example 10.9. Redirecting output from Mark Pilgrim's Dive into Python.

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.

edit flag offensive delete link more
2

answered 2010-10-22 18:51:27 -0500

ccanonc gravatar image
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.
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2010-10-22 15:19:35 -0500

Seen: 6,126 times

Last updated: Oct 23 '10