Paths and directories
A path is a string, and the Path module of Elixir is the set of functions that build and take one apart. The folders a path names are the calls of File: ls to read a directory, mkdir_p to make one and rm_rf to remove a tree. The files lesson covered reading and writing; this page is about where those files live.
Path.join
Path.join/2 puts the separator between its parts and Path.join/1 takes a list. It does not read the filesystem, so it works the same for a path that exists and for one that is about to:
join.exs
IO.puts(Path.join("/tmp", "notes.txt")) IO.puts(Path.join(["a", "b", "c"])) IO.puts(Path.join("a", "/b"))
elixir join.exs
/tmp/notes.txt
a/b/c
a/b
The third line is the surprise: a leading slash in the middle of a join is not an absolute path, and Path.join("a", "/b") is a/b rather than /b. A program that builds a path from user input joins the parts and then checks the result, because the join alone does not keep a value inside a folder.
basename, dirname, extname and rootname
The four functions read the four questions a path answers: the file name, the folder, the extension and the name without it. None of them touches the disk:
parts.exs
path = "/tmp/reports/notes.txt" IO.puts(Path.basename(path)) IO.puts(Path.dirname(path)) IO.puts(Path.extname(path)) IO.puts(Path.rootname(path))
elixir parts.exs
notes.txt
/tmp/reports
.txt
/tmp/reports/notes
Path.basename/2 takes the extension to strip, so Path.basename(path, ".txt") is notes, and Path.rootname/1 is the same answer for the common case. A file name built from a value goes through Path.basename/1 first, which drops any folder the value carries and keeps a name from escaping the directory.
ls, mkdir_p and rm_rf
File.ls!/1 answers the names in a folder, File.mkdir_p!/1 makes a folder and every parent it needs, and File.rm_rf!/1 removes a folder with its contents. The program below builds a small tree in the temporary folder, reads it back and removes it:
dirs.exs
root = Path.join(System.tmp_dir!(), "elixir_paths_demo") File.mkdir_p!(Path.join(root, "reports")) File.write!(Path.join(root, "notes.txt"), "hi") IO.inspect(File.ls!(root) |> Enum.sort()) IO.puts(File.dir?(root)) IO.puts(File.exists?(Path.join(root, "reports"))) File.rm_rf!(root)
elixir dirs.exs
["notes.txt", "reports"]
true
true
File.ls!/1 answers a list in the order the directory holds it, so a program that shows the names sorts them. File.dir?/1 is the check for a folder and File.exists?/1 for either kind. The bang in the names follows the module: the plain version answers a tuple and the bang raises.
A folder for a test
A test that writes files must not write into the project, and the temporary folder of the system is where it works. A name built from System.unique_integer/1 keeps two runs apart:
temp.exs
dir = Path.join(System.tmp_dir!(), "elixir_tmp_#{System.unique_integer([:positive])}") File.mkdir_p!(dir) IO.puts(File.dir?(dir)) File.rm_rf!(dir) IO.puts(File.exists?(dir))
elixir temp.exs
true
false
System.tmp_dir!/0 answers the folder the operating system gives for temporary files, and the name of the program is part of it so a leftover is recognisable. ExUnit gives a test the same shape through @tag :tmp_dir, which the testing part of the course uses; a script that writes a file in the current folder instead leaves it in whatever folder the reader ran it from.
The mistake: a folder that is not there
The listing calls take a folder that must exist. Given a name that is not there, the program stops with a File.Error that names the directory it tried:
mistake.exs
IO.inspect(File.ls!("missing_dir"))
elixir mistake.exs
** (File.Error) could not list directory "missing_dir": no such file or directory
(elixir 1.19.7) lib/file.ex:2064: File.ls!/1
mistake.exs:1: (file)
The plain File.ls/1 answers {:error, :enoent} instead, which is the form a program uses when the folder may legitimately be absent. The fix for the bang call is not to rescue it but to ask first, File.dir?/1, or to create the folder with File.mkdir_p!/1 before the listing.
The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
Path.join/2do with a part that starts with a slash?Answer
it joins it like any other part, so "a" joined with "/b" is "a/b"
The join is a text operation, and the leading slash does not reset the path.
-
Which call creates a folder and every parent it needs?
Answer
File.mkdir_p/1
The p stands for parents: the call makes the levels that are missing.
-
What does
Path.extname/1answer for "/tmp/reports/notes.txt"?Answer
.txt
The extension keeps its dot, and Path.rootname/1 is the name without it.
-
Why does a test write into
System.tmp_dir!()?Answer
so the test does not write into the project or a shared folder
The system temporary folder keeps a test run away from the files of the project.
-
What does this program print?
IO.puts(Path.join("/tmp", "notes.txt"))Answer
/tmp/notes.txt
The join puts one separator between the folder and the name.
-
What does this program print?
IO.puts(Path.basename("/tmp/reports/notes.txt"))Answer
notes.txt
The basename is the last part of the path.
-
What does this program print?
IO.puts(Path.dirname("/tmp/reports/notes.txt"))Answer
/tmp/reports
The dirname is everything before the last separator.
-
What does this program print?
root = Path.join(System.tmp_dir!(), "demo") File.mkdir_p!(Path.join(root, "a")) IO.inspect(File.ls!(root)) File.rm_rf!(root)Answer
["a"]
The folder holds the one directory that was made in it.
-
What does
File.ls/1answer for a folder that is not there?Answer
{:error, :enoent}
The plain call answers the pair; the bang call raises the File.Error.
-
What does this program do?
IO.inspect(File.ls!("missing_dir"))Answer
A File.Error, because the directory does not exist
The bang call raises when the listing fails, and the message names the directory.
Article author: Arthur Isaev