Course of Raku / Objects, I/O, and exceptions / Input and output / Working with directories / Exercises / Does it exist?
Solution: Does it exist?
Here is a possible solution to the task.
Code
say 'test.txt'.IO.e;
spurt 'test.txt', 'x';
say 'test.txt'.IO.e;🦋 You can find the source code in the file does-it-exist.raku.
Output
False
TrueComments
.etests whether a path exists. The first check runs before the file is created, so it returnsFalse.spurtthen createstest.txt, and the second check on the same path now returnsTrue. Asking the path object again reflects the current state of the file system.