# \`open\` and \`include\` might point to different folders

**URL:** <https://discourse.julialang.org/t/open-and-include-might-point-to-different-folders/114538>\
**Category:** General Usage\
**Tags:** filesystem, file\
**Created:** [May 22, 2024, 12:37am UTC](https://discourse.julialang.org/t/open-and-include-might-point-to-different-folders/114538 "2024-05-22T00:37:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 22, 2024, 12:37am UTC](https://discourse.julialang.org/t/open-and-include-might-point-to-different-folders/114538/1 "2024-05-22T00:37:45Z")

</div>

So, I have this set-up:

- I have my active Julia folder in VSCode.
- Within it I have a folder “subfolder”.
- I am running stuff in the Julia file “subfolder/my\_script.jl”

I am running this script:

```julia
open("testfile.jl", "w") do file
    write(file, "println(\"Hello world\")")
end
include("testfile.jl")

```

Thie yields an error. Turns out that `open` write the file to the main (active) folder, while `include` checks relative to where `my_script.jl` is (that is, another folder). Its there a way to deal with this easily (i.e. make them generally point to the same folder). Pressumably there is a good reason for this behaviour (but what?).

Taking a step back, I have a Julia structure `my_struct`. I have a function `custom_serialise(my_struct)` which creates a .jl file that when evaluated regenerates `my_struct` for later on. It also writes it in a nice human-readable format with some annotation/

At the end of `custom_serialise(my_struct)` I wanted a safety check, which basically loads the written structure and checks that it is equivalent to the one I was meaning to save. However. if the user runs `custom_serialise` in a file that is different from their active folder, I get a problem with this behaviour, and I am not sure how to deal with it.

---

<div class="post-metadata">

**Author:** ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)\
**Post date:** [May 22, 2024, 2:07am UTC](https://discourse.julialang.org/t/open-and-include-might-point-to-different-folders/114538/2 "2024-05-22T02:07:29Z")

</div>

You can use `include(joinpath(pwd(), "testfile.jl"))` to include relative to the active folder.  
You may want to use `eval(Meta.parse(read("testfile.jl",String)))` instead of `include` [Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/#Program-representation)  
Or use something like JSON3.jl or TOML.jl to serialize your struct more safely.

---

<div class="post-metadata">

**Author:** ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)\
**Post date:** [May 22, 2024, 2:40am UTC](https://discourse.julialang.org/t/open-and-include-might-point-to-different-folders/114538/3 "2024-05-22T02:40:02Z")

</div>

Thanks a lot! 🙂 That works

---

<div class="post-metadata">

**Author:** ![kimikage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kimikage/32/14534_2.png) [@kimikage](https://discourse.julialang.org/u/kimikage)\
**Post date:** [May 22, 2024, 3:15am UTC](https://discourse.julialang.org/t/open-and-include-might-point-to-different-folders/114538/4 "2024-05-22T03:15:40Z")

</div>

Although your problem seems to have been solved, here is the idiom for specifying a path relative to the jl file (not working directory).

```julia
joinpath(@ __DIR__ , "testfile.jl")

```
