【发布时间】:2021-09-04 18:18:23
【问题描述】:
我需要从终端收集所有内容并将其写入 txt 文件。朱莉娅语言 enter image description here
【问题讨论】:
-
你的想象似乎是一个黑色方块
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
标签: julia
我需要从终端收集所有内容并将其写入 txt 文件。朱莉娅语言 enter image description here
【问题讨论】:
标签: julia
在 Julia 1.7 中,您可以这样做:
redirect_stdio(stdout="stdout.txt", stderr="stderr.txt") do
println("hello")
println(stderr, "hello error")
end
在较旧的 Julia 版本中,您需要自己实现此功能(代码取自 https://discourse.julialang.org/t/redirect-stdout-and-stderr/13424/3):
function redirect_to_files(dofunc, outfile, errfile)
open(outfile, "w") do out
open(errfile, "w") do err
redirect_stdout(out) do
redirect_stderr(err) do
dofunc()
end
end
end
end
end
现在可以用作:
redirect_to_files("stdout.txt","stderr.txt") do
println("hello")
println(stderr, "hello error")
end
如果您不想使用do 语法,您也可以自己打开和关闭流。
【讨论】:
stdout.txt 和 stderr.txt 只是文本文件。如果您想同时做这两件事,您可能需要使用 TranscodingStreams.jl,并创建一个不进行编码但将字节发送到控制台的流转码器。