Ask Your Question

Export Answer To Text File

asked 2017-05-30 19:18:16 -0500

happys5 gravatar image


I have read the available suggestions and they did not work.

I would like to output the number generated by 2^1000 to a text file so that I can import to my C++ program.

Please help me along.

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted

answered 2017-06-02 14:52:31 -0500

dom gravatar image

Make a dom.sage file containing :

s = str(2^1000)
f = open('filename.txt', 'w')

Then run using sage :

sage dom.sage

and you get a file 'filename.txt' which contains :


on a Linux operating system (with sage locally installed).

For a Windows operating system (I guess from "C: syntax") I don't know.

edit flag offensive delete link more

answered 2017-05-31 00:22:07 -0500

Sébastien gravatar image

updated 2017-05-31 15:37:54 -0500


sage: s = str(2^1000)
sage: f = open('filename.txt', 'w')
sage: f.write(s)
sage: f.close()

or using a with-statement which closes the file for you:

sage: with open('filename.txt', 'w') as f:
....:     f.write(str(2^1000))
edit flag offensive delete link more


I am still making a mistake. I have tried single backslash and double backslash with no luck. I have also tried capital "C" and lower case "c". My code mirrors yours other than that.

sage: s = str(2^1000)
sage: f = open('C:\Users\miram\OneDrive\Documents\Sage\test.txt', 'w')
sage: f.write(s)
sage: f.close()

Any advice?

happys5 gravatar imagehappys5 ( 2017-05-31 21:06:25 -0500 )edit

There must be something wrong with the file path; try something simpler, like just f=open('test.txt', 'w') to see if that works. I think you can use the command pwd from the sage prompt to show the current working directory as a path.

niles gravatar imageniles ( 2017-06-02 15:09:16 -0500 )edit

answered 2017-05-31 00:26:18 -0500

mforets gravatar image

from the command line:

$ sage -c 'print(2^1000)' >> 2_power_1000.txt
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


Asked: 2017-05-30 19:18:16 -0500

Seen: 92 times

Last updated: May 31