Course of Raku / Objects, I/O, and exceptions / Input and output / Working with directories / Exercises / Make a directory
Solution: Make a directory
Here is a possible solution to the task.
Code
mkdir 'reports';
spurt 'reports/data.txt', 'x';
say 'reports'.IO.d;
say 'reports/data.txt'.IO.e;🦋 You can find the source code in the file make-a-directory.raku.
Output
True
TrueComments
mkdircreates the directory, and.IO.dconfirms thatreportsis a directory.Because the directory now exists, we can write a file into it by giving a path that includes the directory name,
reports/data.txt. Checking.eon that path confirms the file was created inside the new directory.The order matters:
spurtdoes not create missing directories for you. Writing toreports/data.txtbefore thereportsdirectory exists fails with an error likeFailed to open file … : No such file or directory. Making the directory first — asmkdirdoes here — is what lets the write succeed. Conveniently,mkdiralso creates any missing intermediate directories, so a nested path such asmkdir 'reports/2026'builds the whole chain in a single call.