ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 02 Jun 2017 15:09:16 -0500Export Answer To Text Filehttps://ask.sagemath.org/question/37745/export-answer-to-text-file/Hello,
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. Tue, 30 May 2017 19:18:16 -0500https://ask.sagemath.org/question/37745/export-answer-to-text-file/Answer by dom for <p>Hello, </p>
<p>I have read the available suggestions and they did not work. </p>
<p>I would like to output the number generated by 2^1000 to a text file so that I can import to my C++ program. </p>
<p>Please help me along. </p>
https://ask.sagemath.org/question/37745/export-answer-to-text-file/?answer=37770#post-id-37770Make a dom.sage file containing :
> s = str(2^1000)
> f = open('filename.txt', 'w')
> f.write(s)
> f.close()
Then run using sage :
> sage dom.sage
and you get a file 'filename.txt' which contains :
> 0715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
on a Linux operating system (with sage locally installed).
For a Windows operating system (I guess from "C: syntax") I don't know.
Fri, 02 Jun 2017 14:52:31 -0500https://ask.sagemath.org/question/37745/export-answer-to-text-file/?answer=37770#post-id-37770Answer by Sébastien for <p>Hello, </p>
<p>I have read the available suggestions and they did not work. </p>
<p>I would like to output the number generated by 2^1000 to a text file so that I can import to my C++ program. </p>
<p>Please help me along. </p>
https://ask.sagemath.org/question/37745/export-answer-to-text-file/?answer=37747#post-id-37747:
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))Wed, 31 May 2017 00:22:07 -0500https://ask.sagemath.org/question/37745/export-answer-to-text-file/?answer=37747#post-id-37747Comment by niles for <p>:</p>
<pre><code>sage: s = str(2^1000)
sage: f = open('filename.txt', 'w')
sage: f.write(s)
sage: f.close()
</code></pre>
<p>or using a with-statement which closes the file for you:</p>
<pre><code>sage: with open('filename.txt', 'w') as f:
....: f.write(str(2^1000))
</code></pre>
https://ask.sagemath.org/question/37745/export-answer-to-text-file/?comment=37771#post-id-37771There 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.Fri, 02 Jun 2017 15:09:16 -0500https://ask.sagemath.org/question/37745/export-answer-to-text-file/?comment=37771#post-id-37771Comment by happys5 for <p>:</p>
<pre><code>sage: s = str(2^1000)
sage: f = open('filename.txt', 'w')
sage: f.write(s)
sage: f.close()
</code></pre>
<p>or using a with-statement which closes the file for you:</p>
<pre><code>sage: with open('filename.txt', 'w') as f:
....: f.write(str(2^1000))
</code></pre>
https://ask.sagemath.org/question/37745/export-answer-to-text-file/?comment=37754#post-id-37754I 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.
<pre>
sage: s = str(2^1000)
sage: f = open('C:\Users\miram\OneDrive\Documents\Sage\test.txt', 'w')
sage: f.write(s)
sage: f.close()
<code>
Any advice?Wed, 31 May 2017 21:06:25 -0500https://ask.sagemath.org/question/37745/export-answer-to-text-file/?comment=37754#post-id-37754Answer by mforets for <p>Hello, </p>
<p>I have read the available suggestions and they did not work. </p>
<p>I would like to output the number generated by 2^1000 to a text file so that I can import to my C++ program. </p>
<p>Please help me along. </p>
https://ask.sagemath.org/question/37745/export-answer-to-text-file/?answer=37748#post-id-37748from [the command line](http://doc.sagemath.org/html/en/reference/repl/options.html#command-line-options-for-sage):
$ sage -c 'print(2^1000)' >> 2_power_1000.txt
Wed, 31 May 2017 00:26:18 -0500https://ask.sagemath.org/question/37745/export-answer-to-text-file/?answer=37748#post-id-37748