Course of Raku / Objects, I/O, and exceptions / Input and output / Working with directories
Listing a directory
The dir routine returns the entries of a directory —
each as a path object. Their order is not fixed, so it is common to sort
the result for predictable output.
mkdir 'box';
spurt 'box/a.txt', '';
spurt 'box/b.txt', '';
for dir('box').sort -> $entry {
say $entry.basename;
}This prints the names of the files in the box directory,
in order:
a.txt
b.txtEach entry is a full path object; the basename method
gives just the final name, without the directory part. From a path
object you can ask all the same questions as before — .f,
.d, .e — so you can, for instance, walk
through a directory and pick out only the files.
Practice
Complete the quiz that covers the contents of this topic.
Course navigation
← Working with directories | Quiz — File tests →
💪 Or jump directly to
the exercises in this section.