# Can I change the working directory in a file more than once?

**URL:** <https://discourse.julialang.org/t/can-i-change-the-working-directory-in-a-file-more-than-once/80839>\
**Category:** General Usage\
**Tags:** filesystem\
**Created:** [May 10, 2022, 7:31pm UTC](https://discourse.julialang.org/t/can-i-change-the-working-directory-in-a-file-more-than-once/80839 "2022-05-10T19:31:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [May 10, 2022, 7:31pm UTC](https://discourse.julialang.org/t/can-i-change-the-working-directory-in-a-file-more-than-once/80839/1 "2022-05-10T19:31:09Z")

</div>

Hey folks, I was having a little trouble getting some julia file system functions to work. The code seems correct, so I can’t tell if I made a code error, or if it is an issue with the vscode plugin, etc?

I have a julia `.jl` file, and I set the working directory to the directory of the current file. Then I changed the working directory to a different directory that contains some “include” files. For some reason the include statement is still referring to the original working directory instead of the changed working directory.

Here is some sample code. Note that the relative directory work works and is good–no problems with the relative path.

```julia
cd(@ __DIR__ )

using Zygote
using Plots
gr()

cd(joinpath(@ __DIR__ , "../..", "opt_problems/"))

include("unconstrained_problems.jl")
include("regression_problems.jl")

```

The error I get is `ERROR: SystemError: opening file "/.../julia/julia_opt/algfo_polished/3_bracketing/unconstrained_problems.jl": No such file or directory`. `

So the error is referencing the original working directory instead of the `opt_problems` directory.

Is there some restriction on changing directories in a file, or am I only allowed to change the directory once?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 10, 2022, 7:47pm UTC](https://discourse.julialang.org/t/can-i-change-the-working-directory-in-a-file-more-than-once/80839/2 "2022-05-10T19:47:37Z")

</div>

`include` is relative to the directory of the current _file_, not relative to the current working directory.

So you can do:

```julia
include("../../opt_problems/unconstrained_problems.jl")
include("../../opt_problems/regression_problems.jl")

```

with no `cd` and no `@ __DIR__ ` at all.

---

<div class="post-metadata">

**Author:** ![00krishna](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/00krishna/32/8843_2.png) [@00krishna](https://discourse.julialang.org/u/00krishna)\
**Post date:** [May 10, 2022, 7:50pm UTC](https://discourse.julialang.org/t/can-i-change-the-working-directory-in-a-file-more-than-once/80839/3 "2022-05-10T19:50:05Z")

</div>

Ahh, so that makes sense. Thanks @stevengj that is very helpful. I just made the wrong assumption.
