write_text
Builtin index · Collections & strings
result = write_text(path, s)
Input: path, s · Output: result
What it is
path and s are Strs (file path, content to write). Writes the whole string to a file, overwriting it.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = write_text(path, s) | path Strnot documented yets Strnot documented yet | number —bytes written | Returns a number (bytes written). append_text adds to the end instead; the counterpart of read_all_text. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
out_path = "/tmp/qu_scratch_note.txt"
bytes = write_text(out_path, "hello from Qu\n")
print(bytes) # 14
from collections-strings
path = "/tmp/qu_scratch_append.txt"
write_text(path, "first line\n")
append_text(path, "second line\n")
print(read_all_text(path)) # "first line\nsecond line\n"
from collections-strings
mkdir("list_demo")
write_text("list_demo/report.qu", "print(1)")
write_text("list_demo/plot.qu", "print(2)")
write_text("list_demo/notes.txt", "the needle is here")
make_file("list_demo/empty.log")
qu_files = list_files("list_demo", "*.qu")
print("{length(qu_files)} Qu files")
carrying = list_files("list_demo/*", contains = "needle")
for f in carrying
print("carries the needle: {f}")
end for
blank = list_files("list_demo/*", size = "0")
print("{length(blank)} empty file")
2 Qu files carries the needle: list_demo\notes.txt 1 empty file
from file-io
How it works
write_text(path, s) / append_text(path, s) — the writing counterparts of read_all_text, which existed alone. Saving a string should not need three calls and a handle. append_all(path, s) is append_text under the name that pairs with read_all_text: whole file in, whole string out, no handle. Two spellings rather than a rename, since append_text is already in scripts.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.