What is file manipulation in Elixir?

The File module includes many useful functions to manipulate files using Elixir.

Let's explore how to perform the most common functions while using built-in Elixir functions:

  • Create and write a new file

  • Read an existing file

  • Stream the content of an existing file

  • Rename a preexisting file

  • Delete a file

Scenario 1: Create and write a new file

We'll focus on creating and filling a text file in this scenario. The syntax of the write command is as follows:

File.write(path,content,options)

This command writes the content to the file specified in the path. The options are optional parameters specifying the encoding of the resulting file content. The file is automatically created if it does not exist. When running successfully, this command returns:ok. A tuple {:error, reason} is returned if a problem occurs.

Example 1

#Define the file content
file_content = "Hello World"
#Run the command
output = File.write("/usercode/output/test.txt", file_content)
#Display the output
IO.puts output

Explanation

Let's explain the code above.

  • Line 2: Define the file content.

  • Line 4: Execute the File.write command to create a text file and write the specified content into it. By default, the already existing file content is overwritten when running this command.

  • Line 6: Display the output status of the command.

Example 2

Within the same context, let's now try to append some text to an existing file while using the :append option.

#Define the file content
file_content = "Hello World"
#Run the command
_output = File.write("/usercode/output/test.txt", file_content,[:append])
output = File.write("/usercode/output/test.txt", "\nappended info",[:append])
#Display the output
IO.puts output
#Display the content of the resulting file
IO.puts File.read!("/usercode/output/test.txt")

Explanation

Let's explain the code above.

  • Line 2: Define the file content.

  • Line 4: Execute the File.write command to create a text file and write the specified content into it.

  • Line 5: Execute the File.write command with the :append option in order to write additional content to the text file created.

  • Line 7: Display the output status of the command.

  • Line 9: Read and display the content of the resulting file.

Scenario 2: Read an existing file

In this scenario, we will describe two variants of the read command.

The syntax of the first variant command is as follows:

File.read(path)

The path maps the file being read. When executed successfully, this command returns a tuple {:ok, content} where content is a binary data object containing the file content. If a problem occurs, a tuple {:error, reason} is returned.

Example 3

main.exs
test.txt
#Read the sample file "test.txt"
{:ok,content} = File.read("test.txt")
#Display the file content
IO.puts content

Explanation

Let's explain the code above:

  • Line 2: Read a sample file named test.txt.

  • Line 4: Display the content of the given file.

The syntax of the other variant is as follows:

File.read!(path)

This command returns directly the binary content of a given filename.
If an error occurs a File.Error exception is returned.

Example 4

main.exs
test.txt
#Read the sample file "test.txt"
content = File.read!("test.txt")
#Display the file content
IO.puts content

Explanation

Let's explain the code above:

  • Line 2: The File.read! command returned the content of the sample file test.txt.

  • Line 4: Display the content of the given file.

Scenario 3:Stream the content of an existing file

The streaming function comes into play when reading large files to offload resources like network resources. The chosen file will be streamed line by line instead of being loaded entirely in the memory.

The syntax of this command is:

File.stream(path,modes,line_or_bytes)

The path maps the file being read line by line. The modes parameter refers to the file modes (i.e.: raw, read_ahead, binary). The line_or_bytes parameter indicates whether to read a line or a given number of bytes. When executed successfully, this command returns a stream as a result.

Example 5

Let's see how it works.

main.exs
test.txt
File.stream!("test.txt",line_or_bytes: :line)
|> Enum.each(&IO.puts /1)

Explanation

Let's explain the code above:

  • Line 1: Stream the content of a sample file named test.txt line by line.

  • Line 2: Display the content of the streamed line.

Scenario 4: Rename a preexisting file

This command serves for renaming a file in the file system.

The syntax of this command is as follows:

File.rename(source, destination)

This function may serve also to move files (and directories) between directories. When running successfully, this command returns :ok. If a problem occurs, a tuple {:error, reason} is returned.

Example 6

Let's look at the command below.

main.exs
test.txt
Test data
Testing data

Explanation

We executed the following steps in the code widget above:

  • Line 1: Rename the file test.txt to testing.txt.

  • Line 2: Display the output status of this command.

Scenario 5: Delete a file

To remove a file, which is no longer needed from the file system, we may use the File.rm function. The syntax of this command is as follows:

File.rm(path)

When running successfully, this command returns :ok. If a problem occurs, a tuple {:error, reason} is returned.

Example 7

Let's test it below.

main.exs
test.txt
output = File.rm("test.txt")
IO.puts output

Explanation

We performed the following steps in the code widget above:

  • Line 1: Remove the file test.txt permanently.

  • Line 2: Display the output status of this command.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved