Ask Your Question
7

How to output from sage

asked 14 years ago

schof gravatar image

updated 14 years ago

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.

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
5

answered 14 years ago

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.

Preview: (hide)
link
2

answered 14 years ago

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.
Preview: (hide)
link

Comments

This is out of date, see here, may be you will get an updated way to do that...

c05772 gravatar imagec05772 ( 2 years ago )
2

answered 14 years ago

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.

Preview: (hide)
link

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: 14 years ago

Seen: 29,303 times

Last updated: Oct 23 '10