朱莉娅:相对于脚本位置,创建一个新文件夹和文件



您是否碰巧知道如何在朱莉娅脚本中获取朱莉娅脚本的路径?

本质上,我构建了一个名为 someCode.jl的朱莉娅脚本,它位于名为 repository的文件夹中。

运行someCode.jl时,我想在文件夹中创建一个名为output的输出文件夹,然后将文件a.txt放入文件夹output

有了一个绝对的路径,它可以正常工作,但是我希望将其用于不同路径的人可以运行,但是我还找不到正确的语法。

尝试

pwd()命令返回安装朱莉娅的目录,在这种情况下是:"E:\Users\a\AppData\Local\Julia-1.1.0"因此,我应该确保当前路径在我运行时将当前路径更新到脚本所在的文件夹,或者获取脚本本身的位置,在脚本代码中。两种方法尚未成功。

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 CC_13 include(&quort; c:/repository/somelocalcode.jl&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:repositorysomeLocalCode.jl:6

绝对路径确实起作用。

以下是选项:

  • @__DIR__宏扩展到一个带有包含macrocall文件目录的绝对路径的字符串
  • @__FILE__宏扩展到带有包含macrocall的文件路径的字符串
  • PROGRAM_FILE常数包含命令行传递给朱莉娅的脚本名称

非常感谢@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

最新更新