Ask Your Question
7

How to output from sage

asked 2010-10-22 22:19:35 +0200

schof gravatar image

updated 2010-10-22 22:20:17 +0200

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-23 01:12:06 +0200

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 01:51:27 +0200

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
2

answered 2010-10-23 07:01:35 +0200

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

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2010-10-22 22:19:35 +0200

Seen: 9,482 times

Last updated: Oct 23 '10