# Save output as a text file (not in a mathematical sense)

How can I save an output directly as a text file (without copy paste)?

Save output as a text file (not in a mathematical sense)

How can I save an output directly as a text file (without copy paste)?

add a comment

3

You can use `write`

. For instance, the following will create the file `/tmp/file.txt`

containing `[1, 2, 3, 4, 5]`

.

```
sage: L = [1,2,3,4,5]
sage: with open("/tmp/file.txt", "w") as f:
....: f.write(str(L))
....:
```

Converting `L`

to its string representation using `str`

as above does not guarantee that you'll be able to reconstruct the object afterwards when you read the file. For instance, this is the case if you save a polynomial ring to a file. The solution is to use `dumps`

that allows you to recover the object you are saving using `loads`

:

```
sage: R.<x> = ZZ[]
sage: with open("/tmp/file2.txt", "w") as f:
....: f.write(dumps(R))
....:
```

Then `/tmp/file2.txt`

contains a strange-looking string (```
xk`J.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸àrA@)®BF
dC2óJRÓSÀÚ¸<!:&Í BæPÆ
¿Î B=
```

) but you can load the polynomial ring that was saved afterwards:

```
sage: with open("/tmp/file2.txt") as f:
....: S = loads(f.read())
....:
sage: S
Univariate Polynomial Ring in x over Integer Ring
```

And how can I save a text output directly without any converting (executed by Sage)? For example:

```
sage: ZZ[x]
Univariate Polynomial Ring in x over Integer Ring
```

How can I save both lines or at the least the second one as a text file? I know this example is not the best one, but when it comes to outputs of thousands of lines (by one command) it would be useful to have the output as a text file because otherwise I would have to scroll manually (for copy paste).

2

The linux `script`

command?

Before starting sage, start the script. Then everything shown in the console is also written in the file mentioned to script. Then quit sage and the script. This is a valid protocol of the session, using some text editor one can search+edit relevant lines.

Also...

```
[dan@f ~]$ script -c "sage -c \"E = EllipticCurve( QQ, [-65^2,0] ); print E, 'RANK =', E.rank()\"" a.log
Script started, file is a.log
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done, file is a.log
[dan@f ~]$ cat a.log
Script started on Di 27 Jun 2017 18:44:47 CEST
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done on Di 27 Jun 2017 18:44:50 CEST
[dan@f ~]$
```

& CO is possible.

Hi I would like to do that with downloading a file example.sage content of example.sage:

```
print("Hello World")
```

but that does not work ;-(

```
script -c "sage -c \"load("example.sage") \"" a.log
```

I suppose the syntax should be adapted for that ?

```
sage: cat a.log
Script started on Sun 02 Jul 2017 02:23:55 AM EDT
Traceback (most recent call last):
File "/home/sage/sage-7.6/src/bin/sage-eval", line 10, in <module>
eval(compile(s,'<cmdline>','exec'))
File "<cmdline>", line 1, in <module>
NameError: name 'example' is not defined
Script done on Sun 02 Jul 2017 02:23:59 AM EDT
sage:
```

The inner `"example.sage"`

can be changed into `'example.sage'`

to avoid the parsing yoga of `sage -c`

. For instance:

```
[dan@f ~/tmp]$ echo "E = EllipticCurve( QQ, [-65**2,0] ); print E, 'RANK =', E.rank()" > example.sage
[dan@f ~/tmp]$ script -c "sage -c \"load( 'example.sage' ) \"" a.log
Script started, file is a.log
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done, file is a.log
[dan@f ~/tmp]$ cat a.log
Script started on So 23 Jul 2017 17:42:51 CEST
Elliptic Curve defined by y^2 = x^3 - 4225*x over Rational Field RANK = 2
Script done on So 23 Jul 2017 17:42:54 CEST
```

Asked: **
2017-06-22 21:52:44 -0500
**

Seen: **339 times**

Last updated: **Jun 27 '17**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.