Full path to file

I have seen some solutions like @__DIR__ and pwd(), and even some suggest @__FILE__. But these do not do what I really want.

What is it that I want? The location to the file that I currently have open in atom/vscode. Note that the file I have open may not necessarily be in the same directory or folder as I opened via atom/vscode.

I can get the full path to file manually in atom using ctrl+shift+c or shift+alt+c in vscode. But this gets a bit tiring as after I have to type and encapsulate in cd(…) everytime.

I want a solution to avoid extra unnecessary steps that I embed within cd(). Help appreciated.

Do you mean getting the path of an poen file in your editor from inside julia? I’m not sure you can get that, julia itself is oblivious to your editor of choice.

2 Likes

Hi I think that your best solution is

Home = @__DIR__

You can move up by

Home2 = dirname(Home)

Thanks both. So I guess this means I will always have to start julia within the directory that I will be working in.

Can you give us step-by-step instructions that allow us to reproduce what you are doing, and then tell us what the current and the hoped-for outcome is? At the moment, we can’t provide much feedback as we don’t know how exactly you have started your editor and Julia processes, what operating system you are on, etc. There are lots of possible ways and workflows. Are you starting your Julia process from within your editor? Are you starting your editor from within Julia? Are you starting both independently from a GUI desktop? Etc.

2 Likes

@__DIR__ and similar should always give you an answer relative to the current file if you’re using inline evaluation (Alt-Enter by default). If you paste code into the REPL (manually or via Ctrl-Enter) then you’ll also get the REPL behaviour, which of course falls back to pwd.

If you’re seeing different behaviour then that’s a bug and I’d love to hear how to trigger it.

2 Likes

@mgkuhn. @pfitzseb. My system is a windows 10 64. I describe steps for julia-vscode but same thing applies to atom with hydrogen or juno.

To reproduce:

  1. Fire up VScode, and open a new folder that contains a julia project you are interested in working on.
  2. You recall there is file in another folder whose result you want to check. Rather than open up a new folder in VScode and start a new julia session in that folder, you simply open this file and run it (not in its parent folder but in the folder you currently have open).
  3. The problem is that you don’t want this file to write output into your current folder but in the folder where it belongs.
  4. So what you need to do here is change the working directory with cd(…) at the top of that file. You can of course do this manually by copying and pasting the path as I described.
  5. But I am looking for a solution that “automatically” changes the directory using code. Suppose @__DIR__ worked for this file then it would be cd(@__DIR__). The problem here is at @__DIR__ points to the folder you opened not where the file is located.
  6. Lets say a function I make up @__LOC__ worked, then it would be cd(@__LOC__) and it would return the directory to this file, not to the one where I fired up the julia session.

How are you running the file?

In julia-vscode, ctrl + enter or alt enter

Ok, and you’re seeing no difference between Alt-Enter and Ctrl-Enter if you just evaluate @__DIR__? This is what it looks like for me (first Alt-Enter, then Ctrl-Enter):
image

1 Like

Ahhh I know see the difference after looking more carefully at alt + enter. But I am also a bit confused and surprised at dependence of this on which method I use to run the code.