朱莉娅(Julia):在相对于脚本位置的位置创建一个新的文件夹和文件

时间:2019-03-18 13:58:57

标签: path julia

您是否会偶然知道如何在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中,这两种方法都尚未成功。

MWE:

这是示例文件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

绝对路径确实起作用。

2 个答案:

答案 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