# Revision history [back]

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 (xkJ.NLOÕ+ÊÌK/Ö+ÈÏ©ÌËÏÍLÌAbÆäâóóKJKò¸à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:
`