# How do I save the output to .txt file?

**URL:** <https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 14, 2019, 2:30pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270 "2019-12-14T14:30:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 14, 2019, 2:30pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/1 "2019-12-14T14:30:49Z")

</div>

Hey People,

right now I’m struggling with saving my data.

By using a for loop and the println() command, my programm is giving me out a bunch of named arrays like this in the following:

for n = 1:10  
a = rand(1:10, 4)  
println("a$n = ",a)  
end

now my question is:  
how do I save the output from the console (REPL) as a text oder csv file ?

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [December 14, 2019, 2:52pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/2 "2019-12-14T14:52:48Z")

</div>

It is not clear from your question what is exactly your end goal- do you just want to direct `println` statements to a text file? Do you have some array that you want to save to a file? Do you want to save all the output in a REPL session to a file?  
If you could clarify your end goal you might get better help.

In any case, if what you want is to direct `println` statements to a file you could do something like:

```julia
open("file.txt","a") do io
   println(io,"a=",a)
end

```

But I’m not sure that this is really what you want.

---

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 14, 2019, 2:56pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/3 "2019-12-14T14:56:56Z")

</div>

Well, my goal is to save the println() statements to a text file.  
In this example I mentioned, I do receive (in this case 9) arrays, which I would like to have saved in a text file.

---

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 14, 2019, 2:57pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/4 "2019-12-14T14:57:31Z")

</div>

Could you explain what the “do io” command actually does and why there is “a” in the argument of open()?

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [December 14, 2019, 3:08pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/5 "2019-12-14T15:08:50Z")

</div>

Have a look at the [documentation](https://docs.julialang.org/en/v1/base/io-network/#Base.open), which you can also access from the REPL by typing `? open` and `? do`.  
If you still have questions after reading the documentation feel free to ask.

Depending on what exactly it is that you do, I don’t think the optimal solution is to print the arrays into a text file like this. Maybe you can organize all your data in a [dataframe](https://juliadata.github.io/DataFrames.jl/stable/) which you can then more easily manipulate and save/load from file.

You could also use a package like [JLD2](https://github.com/JuliaIO/JLD2.jl) which allows you to save and load Julia objects in a format similar to HDF5.

---

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 14, 2019, 3:28pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/6 "2019-12-14T15:28:31Z")

</div>

well I did check the documentation already, but it is still not really plausible to me what “do io” is actually good for and why it is continued in the argument of write() or println() … .

thanks for your advise, I’ll check out how to work in a dataframe and if there are still any questions I’ll let you know !

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [December 14, 2019, 3:29pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/7 "2019-12-14T15:29:29Z")

</div>

You should read the documentation. But my understanding is that the suggested syntax makes sure that the file is automatically closed/released after `end`.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [December 14, 2019, 5:26pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/8 "2019-12-14T17:26:53Z")

</div>

On the do block, see [Essentials · The Julia Language](https://docs.julialang.org/en/v1/base/base/#do)

How it relates to file IO is described in [Networking and Streams](https://docs.julialang.org/en/v1/manual/networking-and-streams/).

Calling `open` with a function (implicitly created by `do`) is a shortcut for (1) open; (2) call function on the IOStream; (3) close.

---

<div class="post-metadata">

**Author:** ![orialb](https://avatars.discourse-cdn.com/v4/letter/o/65b543/32.png) [@orialb](https://discourse.julialang.org/u/orialb)\
**Post date:** [December 14, 2019, 5:34pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/9 "2019-12-14T17:34:45Z")

</div>

Maybe it will help reading about [anonymous functions](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1) to better understand the `do` syntax.

---

<div class="post-metadata">

**Author:** ![WEEZEERAE](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/weezeerae/32/8918_2.png) [@WEEZEERAE](https://discourse.julialang.org/u/WEEZEERAE)\
**Post date:** [December 15, 2019, 2:29pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/10 "2019-12-15T14:29:52Z")

</div>

thank you so much !  
working with data frames is way more convenient, than anything I tried before.  
It’s like having a flexible storage for a bunch of arrays/data wich can be easily saved !

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 15, 2019, 7:01pm UTC](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/11 "2019-12-15T19:01:38Z")

</div>

If you are using DataFrames, use `CSV.jl` to save things to a text file.
