Coding Rooms is discontinuing Free and K-12 services. Click here for more information.

Understanding Relative File Paths

A quick read for anyone whose code produces the dreaded “FileNotFound” error when attempting to access a data file.

January 10, 2022
by
Kathryn Perry

* I use the term, “directory” throughout this article. For clarification purposes, you may interchange the word, “folder” with “directory”.

File paths are a common stumbling block for novice programmers.

First, what’s the difference between a relative file path and an absolute file path? A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths.

Describing the Path Relative to the Current (Working) Directory

In these Java examples, we define a path to the EQs_last_last_week_of_2021.csv file. The data file is within the geological directory, which is within the data-files directory.

– case where the data-files directory is in the current directory
String filePath = "./data-files/geological/EQs_last_week_of_2021.csv";

– case where the data-files directory up one level (in the parent directory)
String filePath = "../data-files/geological/EQs_last_week_of_2021.csv";

– case where the data-files directory is up two levels (in the grandparent directory)
String filePath = "../../data-files/EQs_last_week_of_2021.csv";

– case where the data-files directory is up three levels (in the great grandparent directory)
String filePath = "../../../data-files/EQs_last_week_of_2021.csv";

Let’s take this example further. We are writing code that accesses a data file (called EQs_last_last_week_of_2021.csv) and reads the earthquake data, one line (one earthquake) at a time. The file is a .csv, which means that values are separated by commas (comma separated values).

Which file path will work? It depends on the file structure.

In figure 1, we use:
String filePath = “../../../data-files/geological/EQs_last_week_of_2021.csv”;

Notice that:

1) The ReadData.java file is inside the BuffemaroonReader-example directory. (current level)

2) The buffemaroonreader-class-examples directory is inside the java-examples directory. (up one level)

3) The java-examples directory is inside the programming-with-data-files directory. (up two levels)

4) The programming-with-data-files directory is in the same directory as the data-files directory. (up three levels)

Figure 1

In figure 2, we use:
String filePath = “../../data-files/geological/EQs_last_week_of_2021.csv”;

Notice that:

5) The ReadData.java file is inside the BuffemaroonReader-example directory. (current level)

6) The buffemaroonreader-class-examples directory is inside the java-examples directory. (up one level)

7) The java-examples directory is in the same directory as the data-files directory. (up two levels)

Figure 2

In figure 3, we use:
String filePath = “../data-files/geological/EQs_last_week_of_2021.csv”;

Notice that:

8) The ReadData.java file is inside the BuffemaroonReader-example directory.

9) The buffemaroonreader-class-examples directory is inside the java-examples directory, which is in the same directory as the data-files directory. (up one level)

Figure 3

By the way, this structure seen in Figure 3 is not good practice. It is silly to put the data-files directory in the java-examples directory because a data file can be accessed using any old programming language. With the data-files directory here in the java-examples directory, it would be a pain to access a data file from files within the python-examples directory.

In figure 4, we use:
String filePath = “./data-files/geological/EQs_last_week_of_2021.csv”;

Notice that:

10) The ReadData.java file and the data-files directory are both inside the programming-with-data-files directory. (current level)

Figure 4

Note that I am compiling and running the ReadData class from within it’s own directory each time (using the CONSOLE). The Coding Rooms RUN button runs from the parent directory. If using the RUN button, the correct file path changes accordingly.


Want to play with file paths and accessing data files? Check out my workspace below. Click “Fork” to get your own copy on Coding Rooms!

Kathryn Perry
Kathryn Perry

Computer Science Teacher
Burnt Hills-Ballston Lake High School
Burnt Hills, New York

January 10, 2022

Learn why these organizations choose Coding Rooms as their next-generation learning platform.