Course of Raku / Objects, I/O, and exceptions / Input and output / Deleting files and directories / Exercises / Clear a directory
Solution: Clear a directory
Here is a possible solution to the task.
Code
mkdir 'cache';
spurt 'cache/a.tmp', '1';
spurt 'cache/b.tmp', '2';
.unlink for 'cache'.IO.dir;
rmdir 'cache';
say 'cache'.IO.e;🦋 You can find the source code in the file clear-a-directory.raku.
Output
FalseComments
rmdironly removes an empty directory, so the files have to go first.'cache'.IO.dirlists the entries, and.unlink for …deletes each one.With the directory now empty,
rmdirremoves it. The final.etest reportsFalse, confirming thatcacheis gone.