Elixir IO & File
The IO module reads and writes to devices such as the terminal, and the File and Path modules work with the file system. File functions follow the usual Elixir convention: a plain version returns {:ok, result} or {:error, reason}, and a bang version (File.read!/1) returns the result directly or raises.
Note: The examples here run in a temporary directory, so they write their files under
/tmp. On your computer you can use any path.
Printing
| Function | Prints |
|---|---|
IO.puts(text) |
text plus a newline |
IO.write(text) |
text without a newline |
IO.inspect(value, opts) |
any value in Elixir syntax; returns the value |
IO.puts(:stderr, text) |
to standard error |
IO.gets("prompt> ") |
reads a line from standard input |
IO.write("Loading")
IO.write("...")
IO.puts(" done")
# IO.puts accepts "iodata": nested lists of strings (and bytes)
IO.puts(["Total: ", Integer.to_string(42), " items"])
IO.inspect(%{id: 7, tags: ["a", "b"]}, label: "record")
IO.inspect(Enum.to_list(1..30), limit: 5)
IO.inspect("tab\there", label: "escaped")Loading... done
Total: 42 items
record: %{id: 7, tags: ["a", "b"]}
[1, 2, 3, 4, 5, ...]
escaped: "tab\there"
iodata (lists of strings) is how Elixir builds output efficiently: instead of concatenating strings, you nest them in a list and the runtime writes the pieces directly.
Writing and reading files
path = "/tmp/notes.txt"
:ok = File.write(path, "first line\n")
:ok = File.write(path, "second line\n", [:append])
IO.inspect(File.read(path))
IO.inspect(File.exists?(path))
{:ok, contents} = File.read(path)
IO.inspect(String.split(contents, "\n", trim: true))
IO.inspect(File.read("/tmp/missing.txt"))
try do
File.read!("/tmp/missing.txt")
rescue
e in File.Error -> IO.puts(Exception.message(e))
end{:ok, "first line\nsecond line\n"}
true
["first line", "second line"]
{:error, :enoent}
could not read file "/tmp/missing.txt": no such file or directory
The error reason is an atom such as :enoent (no such file), :eacces (permission denied) or :eisdir (it's a directory). Use the non-bang version when a missing file is an expected situation, and the bang version when it would be a bug.
Streaming large files
File.stream!/1 reads a file lazily, line by line, so even a multi-gigabyte log never has to fit in memory. Streams can also be written to:
log = "/tmp/access.log"
File.write!(log, """
200 GET /
404 GET /old-page
200 GET /about
500 POST /checkout
200 GET /
""")
errors =
File.stream!(log)
|> Stream.map(&String.trim/1)
|> Stream.filter(&(String.starts_with?(&1, "4") or String.starts_with?(&1, "5")))
|> Enum.to_list()
IO.inspect(errors)
# Write a stream into another file
1..3
|> Stream.map(&"row #{&1}\n")
|> Stream.into(File.stream!("/tmp/rows.txt"))
|> Stream.run()
IO.write(File.read!("/tmp/rows.txt"))["404 GET /old-page", "500 POST /checkout"]
row 1
row 2
row 3
Directories and paths
Path functions only manipulate strings; they never touch the disk. File functions do:
base = "/tmp/exceltad_demo"
File.rm_rf!(base)
File.mkdir_p!(Path.join([base, "reports", "2026"]))
File.write!(Path.join(base, "reports/2026/jan.csv"), "day,sales\n1,120\n")
File.write!(Path.join(base, "reports/2026/feb.csv"), "day,sales\n1,95\n")
File.write!(Path.join(base, "readme.md"), "# Demo\n")
IO.inspect(File.ls!(Path.join(base, "reports/2026")) |> Enum.sort())
IO.inspect(Path.wildcard(Path.join(base, "**/*.csv")) |> Enum.map(&Path.relative_to(&1, base)) |> Enum.sort())
file = "/tmp/exceltad_demo/reports/2026/jan.csv"
IO.inspect(Path.basename(file))
IO.inspect(Path.extname(file))
IO.inspect(Path.rootname(Path.basename(file)))
IO.inspect(Path.dirname(file))
IO.inspect(File.dir?(base))
File.rm_rf!(base)
IO.inspect(File.exists?(base))["feb.csv", "jan.csv"]
["reports/2026/feb.csv", "reports/2026/jan.csv"]
"jan.csv"
".csv"
"jan"
"/tmp/exceltad_demo/reports/2026"
true
false
File.ls!/1 returns names in no particular order, so the example sorts them. File.rm_rf!/1 deletes a directory tree: be careful with it.
Common mistakes
- Matching
{:ok, data} = File.read(path)when the file may be missing: that raises aMatchError. Usecase(orFile.read!if missing really is a bug). - Reading a huge file with
File.read!/1just to process it line by line: useFile.stream!/1. - Building paths with
<>and/:Path.join/2handles separators for you.
Test yourself
What does File.read("/no/such") return?
nil{:error, :enoent}- It raises File.Error
""
Why use File.stream!/1 instead of File.read!/1?
- It is the only way to read text
- It reads lazily line by line, so large files don't need to fit in memory
- It is always faster for small files
- It decodes JSON
Does Path.join("a", "b.txt") create a file?
- Yes
- No, Path functions only work on strings
- Only if the directory exists
- It creates the directory a