Powered by AppSignal & Oban Pro

File Drills

exercises/file_drills.livemd

File Drills

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.8.0", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Return Home Report An Issue

File Drills

Drills help you develop familiarity and muscle memory with syntax through repeated exercises. Unlike usual problems, Drills are not intended to develop problem solving skills, they are purely for developing comfort and speed.

This set of drills is for the File module. Follow the instructions for each drill and complete them as quickly as you can.

File

Use File.ls/1 to list all of the files/folders in the current path.

Use File.ls/1 to list all of the files/folders in the parent directory of the current path.

Use File.mkdir/1 to create a directory called drills.

Use File.dir?/2 to check that drills is a folder.

Use File.write/3 to create an empty file called drills.txt.

Use File.exists?/2 to check that the drills.txt file exists.

Use File.dir?/2 to check that drills.txt is not a folder.

Use File.write/3 to create a filed called hello.txt with the content "world".

Use File.read/1 to read the content of the hello.txt file.

Use File.write/3 to create an empty file in the drills folder you previously created.

Use File.write/3 to create an error/no_entity.txt file that should return {:error, :enoent} because the error folder does not exist.

Use File.write/3 to create a file multi-line.txt with a multi-line string.

multiline_string = """
line 1
line 2
line 3
line 4
line 5
"""

Use File.read/1 to read multi-line.txt.

Use File.stream!/3 to read each line of multi-line.txt and convert it to a list of lines using Enum.to_list/1.

Use File.stream!/3 and Stream.filter/2 to filter in lines from multi-line.txt that contain numbers less than or equal to 3.

Use File.write/3 to re-write multi-line.txt with only the filtered lines.

"""
line 1
line 2
line 3
"""

Use File.open/2, IO.binread/2, and File.close/1 to read the first line of multi-line.txt. Print the value.

Use File.mkdir_p/1 to create:

  • "parent/sub_a/"
  • "parent/sub_b"
  • "parent/sub_c"

Use File.write!/3 to create six empty files:

  • "parent/sub_a/file.txt"
  • "parent/sub_a/file"
  • "parent/sub_b/file.txt"
  • "parent/sub_b/file"
  • "parent/sub_c/file.txt"
  • "parent/sub_c/file"

Use File.ls!/1 to find all of the files/folders inside of the parent folder.

Path

Use Path.join/2 to join "/parent/" and "/child/"

Use Path.join/2 to join "parent" and "child"

Use Path.join/2 to join "folder" and "file.txt".

Use Path.absname/1 to convert the current path "." to an absolute path.

Use Path.dirname/1 to find the directory name of "folder/subfolder/file.txt"

Use Path.dirname/1 to find the directory name of "file.txt".

Use Path.wildcard/2 to find all files in a nested folder "parent/*" that end in a .txt extension. You should see your three file.txt files created earlier.

Use File.rm_rf/1 to delete all folders created by this exercise.

CAUTION: DO NOT DELETE IMPORTANT FILES ON YOUR COMPUTER.

Use File.rm/1 to delete any remaining files created by this exercise.

CAUTION: DO NOT DELETE IMPORTANT FILES ON YOUR COMPUTER.

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "file_drills_reading"
    "exercises" -> "file_drills_exercise"
  end

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, save_name, false)

form =
  Kino.Control.form(
    [
      completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
    ],
    report_changes: true
  )

Task.async(fn ->
  for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
    File.write!(
      progress_path,
      Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
    )
  end
end)

form

Commit Your Progress

Run the following in your command line from the curriculum folder to track and save your progress in a Git commit. Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.

$ git checkout -b file-drills-exercise
$ git add .
$ git commit -m "finish file drills exercise"
$ git push origin file-drills-exercise

Create a pull request from your file-drills-exercise branch to your solutions branch. Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.

DockYard Academy Students Only:

Notify your teacher by including @BrooklinJazz in your PR description to get feedback. You (or your teacher) may merge your PR into your solutions branch after review.

If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.

Up Next

Previous Next
File Save Game