Course of Raku / Objects, I/O, and exceptions / Input and output / Working with files
Appending to a file
By default, spurt replaces whatever was in the file. To
add to the end of an existing file instead, pass the
:append named argument:
spurt 'log.txt', "line1\n";
spurt 'log.txt', "line2\n", :append;
print slurp 'log.txt';The second spurt keeps the existing content and adds the
new line after it, so the file ends up with both lines:
line1
line2Without :append, the second call would have overwritten
the file, leaving only line2. Appending is useful for
things like log files, where each run of a program adds to a growing
record rather than starting over.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Working with files | Quiz — spurt and
slurp →
💪 Or jump directly to the
exercises in this section.