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
True

Comments

  1. .e tests whether a path exists. The first check runs before the file is created, so it returns False.

  2. spurt then creates test.txt, and the second check on the same path now returns True. Asking the path object again reflects the current state of the file system.

Course navigation

Does it exist?   |   Make a directory