Return Julia 中当前目录上方的一个文件夹
Return one folder above current directory in Julia
在 Julia 中,我可以从
获取当前目录
@__DIR__
例如,当我在“当前”文件夹中 运行 以上内容时,它会给我
"/Users/jtheath/Dropbox/Research/Projects/Coding/Current"
但是,我希望它在当前文件夹之上 return 一个文件夹;即,
"/Users/jtheath/Dropbox/Research/Projects/Coding"
有没有在 Julia 脚本中执行此操作的简单方法?
首先,请注意@__DIR__
generally expands to the directory of the current source file (it does however return the current working directory if there are no source files involved, e.g when run from the REPL). In order to reliably get the current working directory, you should rather use pwd()
。
现在回答您的真正问题:我认为获取父目录路径的最简单方法是简单地使用 dirname
:
julia> dirname("/Users/jtheath/Dropbox/Research/Projects/Coding/Current")
"/Users/jtheath/Dropbox/Research/Projects/Coding"
请注意,AFAIU 仅使用字符串操作,并不关心涉及的路径是否实际存在于文件系统中(这就是为什么上面的示例在我的系统上有效,尽管我没有与您相同的文件系统结构) . dirname
对尾部斜杠的 presence/absence 也相对敏感(如果您向它提供直接来自 pwd()
或 @__DIR__
的内容,这应该不是问题)。
我有时也使用这样的东西,希望当我想使用文件系统中实际存在的路径时它可能更健壮:
julia> curdir = pwd()
"/home/francois"
julia> abspath(joinpath(curdir, ".."))
"/home/"
在 Julia 中,我可以从
获取当前目录@__DIR__
例如,当我在“当前”文件夹中 运行 以上内容时,它会给我
"/Users/jtheath/Dropbox/Research/Projects/Coding/Current"
但是,我希望它在当前文件夹之上 return 一个文件夹;即,
"/Users/jtheath/Dropbox/Research/Projects/Coding"
有没有在 Julia 脚本中执行此操作的简单方法?
首先,请注意@__DIR__
generally expands to the directory of the current source file (it does however return the current working directory if there are no source files involved, e.g when run from the REPL). In order to reliably get the current working directory, you should rather use pwd()
。
现在回答您的真正问题:我认为获取父目录路径的最简单方法是简单地使用 dirname
:
julia> dirname("/Users/jtheath/Dropbox/Research/Projects/Coding/Current")
"/Users/jtheath/Dropbox/Research/Projects/Coding"
请注意,AFAIU 仅使用字符串操作,并不关心涉及的路径是否实际存在于文件系统中(这就是为什么上面的示例在我的系统上有效,尽管我没有与您相同的文件系统结构) . dirname
对尾部斜杠的 presence/absence 也相对敏感(如果您向它提供直接来自 pwd()
或 @__DIR__
的内容,这应该不是问题)。
我有时也使用这样的东西,希望当我想使用文件系统中实际存在的路径时它可能更健壮:
julia> curdir = pwd()
"/home/francois"
julia> abspath(joinpath(curdir, ".."))
"/home/"