Julia has native filesystem capabilities that handle the differences between operating systems. They are located in the Filesystem module from the core Base Julia library.

    Whenever you are dealing with files such as CSV, Excel files or other Julia scripts, make sure that your code works on different OS filesystems. This is easily accomplished with the joinpath, @__FILE__ and pkgdir functions.

    If you write your code in a package, you can use to get the root directory of the package. For example, for the Julia Data Science (JDS) package that we use to produce this book, the root directory is:

    As you can see, the code to produce this book was running on a Linux computer. If you’re using a script, you can get the location of the script file via

    The nice thing about these two commands is that they are independent of how the user started Julia. In other words, it doesn’t matter whether the user started the program with julia scripts/script.jl or julia script.jl, in both cases the paths are the same.

    The next step would be to include the relative path from root to our desired file. Since different OS have different ways to construct relative paths with subfolders (some use forward slashes / while other might use backslashes \), we cannot simply concatenate the file’s relative path with the string. For that, we have the joinpath function, which will join different relative paths and filenames according to your specific OS filesystem implementation.

    joinpath also handles subfolders. Let’s now imagine a common situation where you have a folder named data/ in your project’s directory. Inside this folder there is a CSV file named . You can have the same robust representation of the filepath to my_data.csv as:

    It’s a good habit to pick up, because it’s very likely to save problems for you or other people later.