Lean 4 (Meta)programming Cookbook

Listing contents of a directory🔗

To list the contents of a directory, we use the System.FilePath.readDir method on a System.FilePath. This returns an Array IO.FS.DirEntry containing information about each file and subdirectory.

def listDirectory (path : System.FilePath) : IO Unit := do let entries path.readDir for entry in entries do IO.println entry.fileName

Each IO.FS.DirEntry contains the fileName (the name of the file or directory itself) and its full path.