Ask Your Question

Export Answer To Text File

asked 2017-05-31 02:18:16 +0200

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-05-31 07:26:18 +0200

mforets gravatar image

from the command line:

$ sage -c 'print(2^1000)' >> 2_power_1000.txt
edit flag offensive delete link more

answered 2017-05-31 07:22:07 +0200

Sébastien gravatar image

updated 2017-05-31 22:37:54 +0200


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-06-01 04:06:25 +0200 )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 22:09:16 +0200 )edit

answered 2017-06-02 21:52:31 +0200

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

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-31 02:18:16 +0200

Seen: 1,645 times

Last updated: May 31 '17