您是否会偶然知道如何在julia脚本中获取julia脚本的路径?
本质上,我构建了一个名为someCode.jl
的julia脚本,它位于一个名为repository
的文件夹中。
运行someCode.jl
时,我想在文件夹output
中创建名为repository
的输出文件夹,并将文件a.txt
放在文件夹output
中
使用绝对路径,它可以很好地工作,但是我希望它对于那些将其放置在不同路径中的人来说是可运行的,但是我还无法找到正确的语法。
pwd()
命令返回安装julia的目录,在这种情况下为:"E:\\Users\\a\\AppData\\Local\\Julia-1.1.0"
因此,我应该确保当前路径已更新到脚本所在的文件夹。当我运行它,或在脚本代码中获取脚本本身的位置时。在julia REPL中,这两种方法都尚未成功。
这是示例文件someCode.jl
,我通过打开Julia REPL并输入:include("E:/someCode.jl")
open("c:/repository/output/a.txt", "w") do f
n1 = "Content of first line"
write(f, "$n1")
end
如果存储库或输出文件夹尚不存在,则会引发错误:
ERROR: LoadError: SystemError: opening file "c:/repository/output/a.txt": 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("##7#8")), ::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
因此,我确保文件夹c:/repository/output
存在,并在名为'someLocalCode.jl and ran it with
include(“ C:/repository/someLocalCode.jl”)`:
open("c:/repository/output/a.txt", "w") do f
n1 = "Content of first line"
write(f, "$n1")
end
open("output/b.txt", "w") do f
n1 = "Content of first line"
write(f, "$n1")
end
open("/output/b.txt", "w") do f
n1 = "Content of first line"
write(f, "$n1")
end
#include("C:/repository/test.jl")
分别进行测试时,output/b.txt
和/output/b.txt
都产生(本质上)相同的本质:
ERROR: LoadError: SystemError: opening file "/output/b.txt": 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("##15#16")), ::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
绝对路径确实起作用。
答案 0 :(得分:2)
以下是选项:
@__DIR__
宏扩展为具有包含宏调用的文件目录的绝对路径的字符串@__FILE__
宏扩展为带有包含宏调用的文件路径的字符串PROGRAM_FILE
常量,包含从命令行传递给Julia的脚本名称答案 1 :(得分:0)
非常感谢@Bogumil,在示例脚本中实现后,给出的答案可以变成:
# Run this script with the following command:
#include("C:/repository/someCode.jl")
# Print output file to absolute location:
open("c:/repository/output/a.txt", "w") do f
n1 = "Content of first line"
write(f, "$n1")
end
# See what the commands do:
println(@__DIR__)
println(@__FILE__)
println("PROGRAM_FILE=", PROGRAM_FILE)
# Concatenate/merge the local directory with the relative output file location
directoryPath = string(@__DIR__, "/output/b.txt")
println("directoryPath concatenation=", directoryPath)
# Write the output file to a relative file location
open(directoryPath, "w") do f
n1 = "Content of first line"
write(f, "$n1")
end