Is there anyway to reset the current working directory in Julia?

与世无争的帅哥 提交于 2020-04-13 07:41:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!