Calling Reader from Various Directories

Hi all,

I have a function living in a local module, which reads some locally saved files in my package using an path: “src/folder1/…/myfile.txt”. Its purpose is solely to read and return the contents of the file. The problem is that when this function is called somewhere else, say inside of “folder1”, the paths get messed up, because of course “src” doesn’t exist in “folder1”. I’ve looked at similar posts, which talked about @DIR, @MODULE, pathof(), etc, but none of the suggestions solved my problem. Has anyone here run into a similar situation before?

The following works if the directory where the read function is defined and the file to be read are in the same place relative to each other.

If the read function is in src/read.jl and the data is in src/folder1/myfile.txt, you can use

function my_read()
  pathtomyfile = joinpath(__@DIR__, "/folder1/myfile.txt")

(The marcro __@DIR__ expands to the directory where the file it is called in lives.)

1 Like

Yeah, the read function should be defined in the same space relative to the file to be read. However I am calling the read function from a different location. I don’t think this solution works if I am not calling the read function in /src.

Did you try it?