# Calling Reader from Various Directories

**URL:** <https://discourse.julialang.org/t/calling-reader-from-various-directories/45196>\
**Category:** General Usage\
**Created:** [August 19, 2020, 7:14am UTC](https://discourse.julialang.org/t/calling-reader-from-various-directories/45196 "2020-08-19T07:14:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Holdmycoffee](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@Holdmycoffee](https://discourse.julialang.org/u/Holdmycoffee)\
**Post date:** [August 19, 2020, 7:14am UTC](https://discourse.julialang.org/t/calling-reader-from-various-directories/45196/1 "2020-08-19T07:14:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 19, 2020, 8:11am UTC](https://discourse.julialang.org/t/calling-reader-from-various-directories/45196/2 "2020-08-19T08:11:38Z")

</div>

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

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

```

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

---

<div class="post-metadata">

**Author:** ![Holdmycoffee](https://avatars.discourse-cdn.com/v4/letter/h/a9a28c/32.png) [@Holdmycoffee](https://discourse.julialang.org/u/Holdmycoffee)\
**Post date:** [August 19, 2020, 6:28pm UTC](https://discourse.julialang.org/t/calling-reader-from-various-directories/45196/3 "2020-08-19T18:28:33Z")

</div>

> [@thofma](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 19, 2020, 9:06pm UTC](https://discourse.julialang.org/t/calling-reader-from-various-directories/45196/4 "2020-08-19T21:06:01Z")

</div>

Did you try it?
