title: Julia: create a new folder and file in location relative to script location
body: <p>Would you happen to know how to get the path of a julia script, in a julia script?</p>
  <p>In essence, I built a julia script named <code>someCode.jl</code> and it is located in a folder named <code>repository</code>.</p>
  <p>When I run <code>someCode.jl</code>, I would like to create an output folder named <code>output</code> in the folder <code>repository</code> and put a file <code>a.txt</code> in folder <code>output</code>.</p>
  <p>With an absolute path it works fine, but I want it to be runnable for people who put it in a different path, but I have not been able to find the right syntax yet.</p>
  <h1>Attempts</h1>
  <p>The <code>pwd()</code> command returns the directory in which julia is installed, which in this case is: <code>&quot;E:\\Users\\a\\AppData\\Local\\Julia-1.1.0&quot;</code> Hence I should either make sure the current path gets updated to the folder in which the script is located when I run it, or get the location of the script itself, in the script code. Neither approaches have yet been successful in julia REPL.</p>
  <h2>MWE:</h2>
  <p>This is the example file <code>someCode.jl</code>, I run it by opening the Julia REPL and entering: <code>include(&quot;E:/someCode.jl&quot;)</code></p>
  <pre><code>open(&quot;c:/repository/output/a.txt&quot;, &quot;w&quot;) do f
      n1 = &quot;Content of  first line&quot;
      write(f, &quot;$n1&quot;)
  end
  </code></pre>
  <p>If the repository or output folder do not exist yet, it throws error:</p>
  <pre><code>ERROR: LoadError: SystemError: opening file &quot;c:/repository/output/a.txt&quot;: No such file or directory
  Stacktrace:
   [1] #systemerror#43(::Nothing, ::Function, ::String, ::Bool) at .\error.jl:134
   [2] systemerror at .\error.jl:134 [inlined]
   [3] #open#309(::Nothing, ::Nothing, ::Nothing, ::Bool, ::Nothing, ::Function, ::String) at .\iostream.jl:283
   [4] #open at .\none:0 [inlined]
   [5] open(::String, ::String) at .\iostream.jl:339
   [6] #open#310(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::getfield(Main, Symbol(&quot;##7#8&quot;)), ::String, ::Vararg{String,N} where N) at .\iostream.jl:367
   [7] open(::Function, ::String, ::String) at .\iostream.jl:367
   [8] top-level scope at none:0
   [9] include at .\boot.jl:326 [inlined]
   [10] include_relative(::Module, ::String) at .\loading.jl:1038
   [11] include(::Module, ::String) at .\sysimg.jl:29
   [12] include(::String) at .\client.jl:403
   [13] top-level scope at none:0
  in expression starting at E:\someCode.jl:1
  </code></pre>
  <p>So I ensured the folders <code>c:/repository/output</code> exist, put a second script in called 'someLocalCode.jl<code>and ran it with</code>include(&quot;C:/repository/someLocalCode.jl&quot;)`:</p>
  <pre><code>open(&quot;c:/repository/output/a.txt&quot;, &quot;w&quot;) do f
      n1 = &quot;Content of  first line&quot;
      write(f, &quot;$n1&quot;)
  end
  
  open(&quot;output/b.txt&quot;, &quot;w&quot;) do f
      n1 = &quot;Content of  first line&quot;
      write(f, &quot;$n1&quot;)
  end
  
  open(&quot;/output/b.txt&quot;, &quot;w&quot;) do f
      n1 = &quot;Content of  first line&quot;
      write(f, &quot;$n1&quot;)
  end
  
  #include(&quot;C:/repository/test.jl&quot;)
  </code></pre>
  <p>Both <code>output/b.txt</code> and <code>/output/b.txt</code> yield (in essence) the same essence when tested individually:</p>
  <pre><code>ERROR: LoadError: SystemError: opening file &quot;/output/b.txt&quot;: No such file or directory
  Stacktrace:
   [1] #systemerror#43(::Nothing, ::Function, ::String, ::Bool) at .\error.jl:134
   [2] systemerror at .\error.jl:134 [inlined]
   [3] #open#309(::Nothing, ::Nothing, ::Nothing, ::Bool, ::Nothing, ::Function, ::String) at .\iostream.jl:283
   [4] #open at .\none:0 [inlined]
   [5] open(::String, ::String) at .\iostream.jl:339
   [6] #open#310(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::getfield(Main, Symbol(&quot;##15#16&quot;)), ::String, ::Vararg{String,N} where N) at .\iostream.jl:367
   [7] open(::Function, ::String, ::String) at .\iostream.jl:367
   [8] top-level scope at none:0
   [9] include at .\boot.jl:326 [inlined]
   [10] include_relative(::Module, ::String) at .\loading.jl:1038
   [11] include(::Module, ::String) at .\sysimg.jl:29
   [12] include(::String) at .\client.jl:403
   [13] top-level scope at none:0
  in expression starting at C:\repository\someLocalCode.jl:6
  </code></pre>
  <p>Whereas the absolute path does function.</p>