问题
Suppose the current working directory is C:\
(the directory where the .jl file is saved), and then I switch the cwd to some subfolders to perform some tasks.
Is there anyway of directly resetting the cwd back to C:\
after that, i.e. the initial cwd? Or alternatively, is there anyway of locating the directory where the .jl file being run is located, independent of the current working directory? (Without saving the cwd as a variable beforehand)
回答1:
You can use the do keyword together with the cd function:
cd("/some/path") do
pwd() # or do some other work here
end
This will change the working directory to /some/path
, allow you to do some work, and automatically return to the original working directory after the end
keyword.
回答2:
The directory where the current script is located is provided by the @__DIR__ macro.
回答3:
While I would also suggest following @David Varela's do
-syntax advice, if you do specifically want to know the location that the julia binary was originally started from, on (at least) POSIX systems you can find it in ENV["PWD"]
:
julia> pwd()
"/Users/nathan.daly"
julia> cd("Downloads")
julia> pwd()
"/Users/nathan.daly/Downloads"
julia> ENV["PWD"]
"/Users/nathan.daly"
来源:https://stackoverflow.com/questions/59473020/is-there-anyway-to-reset-the-current-working-directory-in-julia