Put

expr>>filename

writes expr to a file.

Put[expr1,expr2,,"filename"]

writes a sequence of expressions expri to a file.

Put["filename"]

creates an empty file with the specified name.

Details and Options

  • Put starts writing output at the beginning of the file. It deletes whatever was previously in the file.
  • Put inserts a newline (line feed) at the end of its output.
  • expr>>filename is equivalent to expr>>"filename". The double quotes can be omitted if the file name is of the form specified in "Operator Input Forms".
  • It is conventional to use names that end with .wl or .m for files containing Wolfram Language input.
  • Put[,OutputStream[]] will append to the given stream.
  • Put works with cloud and local objects.
  • Put by default writes expressions in InputForm.
  • Put[OutputForm[expr],"filename"] generates OutputForm.
  • Put by default uses the character encoding "PrintableASCII" when creating a file. Add the option CharacterEncodingenc to specify a different encoding.
  • When writing to an open file or an OutputStream object, the current encoding of the output stream will be used by default. Add the option CharacterEncodingenc to temporarily change the encoding of the stream for the duration of Put.
  • Put[,File["file"]] is also supported.

Examples

open allclose all

Basic Examples  (1)

Compute a value to store in a temporary file:

Set the current directory to one for temporary files:

Put the value of a into a file:

The file now contains the value:

Reset the directory:

Scope  (4)

Local objects can be used with Put and Get to store expressions persistently:

Cloud objects can be used with Put and Get to store expressions in the cloud:

Put expression in the file specified by the File object:

Create a file and open it for writing:

Put an expression to the stream:

Close the stream:

The file contents were written using the CharacterEncoding option of the stream:

Options  (1)

CharacterEncoding  (1)

By default, the encoding "PrintableASCII" is used when writing to files:

Special characters are written out as long names, which ensures they can be read on any system:

Specify the common encoding "UTF-8":

Special characters are written directly in the encoding, saving space at the cost of portability:

Properties & Relations  (2)

Put overwrites any existing file contents:

Only the second expression is in the file:

Use PutAppend to add to an existing file:

When writing to a stream, Put behaves essentially like PutAppend:

Close the stream:

The file has all three expressions, not just the last one:

Possible Issues  (1)

The form expr>>filename treats filename as a literal name, not an expression to be evaluated:

The file that was written is called "file", not the name contained in the variable file:

Use the form Put[expr,filename] to write to the file whose name is stored in file:

Wolfram Research (1988), Put, Wolfram Language function, https://reference.wolfram.com/language/ref/Put.html (updated 2016).

Text

Wolfram Research (1988), Put, Wolfram Language function, https://reference.wolfram.com/language/ref/Put.html (updated 2016).

CMS

Wolfram Language. 1988. "Put." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/Put.html.

APA

Wolfram Language. (1988). Put. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Put.html

BibTeX

@misc{reference.wolfram_2024_put, author="Wolfram Research", title="{Put}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/Put.html}", note=[Accessed: 03-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_put, organization={Wolfram Research}, title={Put}, year={2016}, url={https://reference.wolfram.com/language/ref/Put.html}, note=[Accessed: 03-December-2024 ]}