To create a file in Julia

Hi! One problem. How do I create a text document and write data to it?

1 Like
io = open("/path/to/file.txt", "w")
println(io, "some text")
close(io)

See the docs
https://docs.julialang.org/en/v1/manual/networking-and-streams/

2 Likes

Depending on what you want to write, maybe even just

julia> x = rand(2,2)
2×2 Array{Float64,2}:
 0.126405  0.14943
 0.260247  0.415338

julia> using DelimitedFiles

julia> writedlm("x.txt", x)

If you’re like me and you always forget to close your files, you can use a do block as well.
This is similar to python’s with command:

open("path/to/file.txt", "w") do io
    print(io, "hello world")
end