在Julia中显示一个文本文件到REPL

问题描述 投票:0回答:3

给定一个文本文件,当前目录中的“hello.jl”:

" Example hello world program."
function hello()
    println("hello, world")
end

你会如何向Julia 1.0.0 REPL显示这个?

这是我到目前为止:

julia> disp(f) = for line in readlines(open(f)); println(line); end
disp (generic function with 1 method)

julia> disp("hello.jl")
" Example hello world program."
function hello()
        println("hello, world")
end

在朱莉娅有没有内置命令来做到这一点?

julia
3个回答
2
投票

在朱莉娅REPL中,命中

;

然后,进入REPL的内置shell模式

shell> head path/to/my/filename

3
投票

你可以使用run函数并在Linux中传递Cmd参数来运行cat系统命令。

键入分号;以更改为shell模式:

shell> cat hello.jl
"Example hello world program."
function hello()
    println("hello, world")
end

使用run函数执行Julia之外的命令:

julia> run(`cat hello.jl`)  # Only works on Unix like systems.
"Example hello world program."
function hello()
    println("hello, world")
end
Process(`cat hello.jl`, ProcessExited(0))

在Windows中,type命令应该类似于Unix cat

julia> show_file(path::AbstractString) = run(@static Sys.isunix() ? `cat $path` : `type $path`)
show_file (generic function with 1 method)

run返回Process对象:

julia> show_file("hello.jl")
"Example hello world program."
function hello()
    println("hello, world")
end
Process(`cat hello.jl`, ProcessExited(0))

在行的末尾使用分号;来抑制REPL中的返回输出:

julia> show_file("hello.jl");  
"Example hello world program."
function hello()
    println("hello, world")
end

或者如果你愿意,你可以在nothing结束时返回show_file


2
投票

println(String(read("hello.jl")))

要么

"hello.jl" |> read |> String |> println

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.