PutAppend¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
PutAppend[expr, "filename"] or expr >>> "filename"
appends expr to the end of the file, creating the file if it does not exist.
PutAppend[expr1, expr2, ..., "filename"]
appends a sequence of expressions, one per line.
Notes
PutAppend works the same as Put, except that it preserves any existing contents of the file rather than truncating them. expr \>\>\> filename is equivalent to expr \>\>\> "filename". Returns Null on success and $Failed if the file cannot be opened.Examples (2)¶
Every input below was run against the current Mathilda build and its output recorded.
Applications (2)¶
In[1]:= Put[x^2 + 1, "/tmp/mathilda_demo.m"]
Out[1]= Null
In[2]:= PutAppend[y, "/tmp/mathilda_demo.m"]
Out[2]= Null
Algorithm¶
readwrite.c - File I/O builtins (Get, Put).
Get reads Mathilda source from a file and evaluates each expression, returning the last value (used by the REPL bootstrap to load the internal .m initialization files).
Put writes one or more expressions to a file in InputForm so the
infix shorthand expr >> "file" and lowers it to Put[expr, "file"].
Implementation notes¶
builtin_putappend shares put_common with Put, differing only in fopen mode: it opens the (last-argument) filename in "a" mode, appending each preceding expression — serialized via expr_to_string (the standard printer) plus a trailing \n — to the end of the file, creating it if absent and never truncating an existing one. PutAppend["file"] writes nothing. Open failure prints PutAppend::noopen and returns $Failed; success returns Null. ATTR_PROTECTED. The infix expr >>> "file" lowers to PutAppend[expr, "file"].
Protected.- Creates the file if it does not exist; otherwise preserves prior contents and appends new lines.
- Returns
Nullon success and$Failedon I/O error.
Attributes: Protected.
References¶
See also: Put
- Source:
src/readwrite.c - Specification:
docs/spec/builtins/file-io.md - Tests:
tests/test_readwrite.c
Notes & additional examples¶
Notes¶
PutAppend[expr, "file"] (equivalently expr >>> "file") adds expr on a new line at the end of the file, creating the file if it does not exist.