path - Julia:在相对于脚本位置的位置创建一个新文件夹和文件

标签 path julia

您是否知道如何在 julia 脚本中获取 julia 脚本的路径?

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

当我运行 someCode.jl 时,我想在文件夹 repository 中创建一个名为 output 的输出文件夹并放置一个文件 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") 运行它p>

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 并使用 include("C:/存储库/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

而绝对路径确实起作用。

最佳答案

以下是选项:

  • @__DIR__ 宏扩展为一个字符串,其中包含宏调用的文件目录的绝对路径
  • @__FILE__ 宏扩展为包含宏调用的文件路径的字符串
  • PROGRAM_FILE 包含从命令行传递给 Julia 的脚本名称的常量

关于path - Julia:在相对于脚本位置的位置创建一个新文件夹和文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55223089/

相关文章:

julia - 在 Julia 中将基于 1 的数组转换为基于 0 的数组

julia - 使用 julia 读取文本文件的最快方法是什么

c# - 如何使用 C# 获取给定路径(可以是目录或文件,甚至是完整路径)的完整路径?

path - SVG <path> 可以同时包含绝对命令和相对命令吗?

regex - 在 Julia 的过滤器中使用正则表达式

julia - 连接 Julia 中的矩阵矩阵

arrays - Julia : create an arrays in for loop

python - 运行 python 脚本,就好像它位于其他地方一样

python - 如何安装ChartDirector?

Python:如何在目录名(不是单个文件名)中搜索特定的 "string"