朱莉娅:在相对于脚本位置的位置中创建一个新的文件夹和文件 [英] Julia: create a new folder and file in location relative to script location
问题描述
您是否会偶然知道如何在julia脚本中获取julia脚本的路径?
Would you happen to know how to get the path of a julia script, in a julia script?
本质上,我构建了一个名为someCode.jl
的julia脚本,它位于名为repository
的文件夹中.
In essence, I built a julia script named someCode.jl
and it is located in a folder named repository
.
运行someCode.jl
时,我想在文件夹repository
中创建一个名为output
的输出文件夹,并将文件a.txt
放在文件夹output
中.
When I run someCode.jl
, I would like to create an output folder named output
in the folder repository
and put a file a.txt
in folder output
.
使用绝对路径,它可以很好地工作,但是我希望它对于那些将其置于不同路径的人来说是可运行的,但是我还无法找到正确的语法.
With an absolute path it works fine, but I want it to be runnable for people who put it in a different path, but I have not been able to find the right syntax yet.
pwd()
命令返回julia的安装目录,在这种情况下为:"E:\\Users\\a\\AppData\\Local\\Julia-1.1.0"
因此,在运行脚本时,我应该确保当前路径已更新为脚本所在的文件夹. ,或获取脚本本身在脚本代码中的位置.这两种方法在朱莉娅·REPL中都还没有成功.
The pwd()
command returns the directory in which julia is installed, which in this case is: "E:\\Users\\a\\AppData\\Local\\Julia-1.1.0"
Hence I should either make sure the current path gets updated to the folder in which the script is located when I run it, or get the location of the script itself, in the script code. Neither approaches have yet been successful in julia REPL.
这是示例文件someCode.jl
,我通过打开Julia REPL并输入以下内容来运行它:include("E:/someCode.jl")
This is the example file someCode.jl
, I run it by opening the Julia REPL and entering: include("E:/someCode.jl")
open("c:/repository/output/a.txt", "w") do f
n1 = "Content of first line"
write(f, "$n1")
end
如果存储库或输出文件夹尚不存在,则会引发错误:
If the repository or output folder do not exist yet, it throws error:
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")`中放置第二个脚本:
So I ensured the folders c:/repository/output
exist, put a second script in called 'someLocalCode.jland 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
都产生(本质上)相同的本质:
Both output/b.txt
and /output/b.txt
yield (in essence) the same essence when tested individually:
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的脚本名称
@__DIR__
macro expanding to a string with the absolute path to the directory of the file containing the macrocall@__FILE__
macro expanding to a string with the path to the file containing the macrocallPROGRAM_FILE
constant containing the script name passed to Julia from the command line
这篇关于朱莉娅:在相对于脚本位置的位置中创建一个新的文件夹和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!