Relative path to file

I’m working in a model with Julia that extracts some data from a spreadsheet. Everything works fine, but I’d like to read the spreadsheet into Julia using a relative path, to be able to run the Julia file from different machines.

I’m using the XLSX.readdata() function. The JL and the XLSX files are together in the same folder.

Is there any way to do this in Julia?

Thank you in advance for any help!

2 Likes

Definitely! What have you tried? Will the file always be in the same folder as the script, do you want to give the path as a command line argument?

See Filesystem · The Julia Language

2 Likes

Thank you for your response!

I have tried using an absolute path and it works fine. The problem comes when I want to run it on a different machine, where I need to change the path to the file.

The idea is that the XLSX file will always be in the same folder together with the JL file.

I have seen the documentation, but still I don’t know how to do it.

Look at https://docs.julialang.org/en/v1/base/file/#Base.@__DIR__

2 Likes

As @Rudi79 said, try this:

filename = "whatever.xlsx"
filepath = joinpath(@__DIR__, filename)
println(filepath)
3 Likes