# How to save multiple output lines to a txt or csv file?

**URL:** https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062
**Category:** New to Julia
**Created:** [January 7, 2020, 1:59pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062 "2020-01-07T13:59:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)
#### Post date: [January 7, 2020, 1:59pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/1 "2020-01-07T13:59:11Z")

</div>

Similar to [How do I save the output to .txt file?](https://discourse.julialang.org/t/how-do-i-save-the-output-to-txt-file/32270/5), I would like to know how to save several output lines into a file.

Currently my function runs a `for` loop, and if certain criteria are met it `println`’s certain values into the `REPL`. I leave it running over night and manually copy the results into an excel sheet. This is obviously not secure and I would like to know how to save it directly into a file.

The last part of the function looks like this, which I had hoped would work.

```julia
        if (sol3[(num_nutrient+num_plant+num_animal+num_vessel_types),end]) > 0.000001 && (sol3[target_species,end] > 0.000001)
            open("Fish parameters 80 species.txt","a") do io #here I think "a" refers to append! because I am adding more to the file 
# This is not clear in the previously linked example because the person also called what they wanted to print as a
# "Fish parameters 80 species.txt" has been previously created
                println(io,"target_species: ", target_species)
                println(io,"catch_max: ", catch_max2)
                println(io,"scaling: ", scaling2)
                println(io,"maintenance: ", maintenance2)
                println(io,"P_catch: ", P_catch2)
                println(io,"μ: ", μ2)
            end
        end

```

So far nothing has been written in the file and I would like to know why.

---

<div class="post-metadata">

### Author: ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)
#### Post date: [January 7, 2020, 2:22pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/2 "2020-01-07T14:22:16Z")

</div>

This looks correct, but I guess you need to call `flush(io)` or `close(io)` at the end, so that the buffer is written out into your file.

---

<div class="post-metadata">

### Author: ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)
#### Post date: [January 7, 2020, 2:29pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/3 "2020-01-07T14:29:30Z")

</div>

Like so?

```julia
if (sol3[(num_nutrient+num_plant+num_animal+num_vessel_types),end]) > 0.000001 && (sol3[target_species,end] > 0.000001)
            open("Fish parameters 80 species.txt","a") do io
                 println(io,"target_species: ", target_species)
                 println(io,"catch_max: ",catch_max2)
                 println(io,"scaling: ",scaling2)
                 println(io,"maintenance: ",maintenance2)
                 println(io,"P_catch: ",P_catch2)
                 println(io,"μ: ",μ2)
            end
           close(io)
        end

```

Or so? (Sorry, it takes a bit to run the function and with the `if` statement I am not sure if it can’t print or if the `if` statement wasn’t fulfilled)

```julia
if (sol3[(num_nutrient+num_plant+num_animal+num_vessel_types),end]) > 0.000001 && (sol3[target_species,end] > 0.000001)
            open("Fish parameters 80 species.txt","a") do io
                 println(io,"target_species: ", target_species)
                 println(io,"catch_max: ",catch_max2)
                 println(io,"scaling: ",scaling2)
                 println(io,"maintenance: ",maintenance2)
                 println(io,"P_catch: ",P_catch2)
                 println(io,"μ: ",μ2)
            close(io)
            end
        end

```

---

<div class="post-metadata">

### Author: ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)
#### Post date: [January 7, 2020, 2:40pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/4 "2020-01-07T14:40:38Z")

</div>

This is a very beginner question, but does the file that you want to write in need to be in your working directory?

Edit: Julia will magically make a document for you in the working directory

It writes when I use the format of

```julia
open("Fish parameters 80 species.csv","a") do io
      println(io,"target_species: ", target_species)
      println(io,"catch_max: ",catch_max2)
      println(io,"scaling: ",scaling2)
      println(io,"maintenance: ",maintenance2)
      println(io,"P_catch: ",P_catch2)
      println(io,"μ: ",μ2)
      close(io)
end

```

Whether this is actually the correct format I don’t know. But it works.

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [January 7, 2020, 5:17pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/5 "2020-01-07T17:17:41Z")

</div>

> [@laborg](#):
>
> This looks correct, but I guess you need to call `flush(io)` or `close(io)` at the end, so that the buffer is written out into your file.

It shouldn’t - when you use the `do` block like this, the buffer is closed at the end of the loop.

> [@HelgavonLichtenstein](#):
>
> This is a very beginner question, but does the file that you want to write in need to be in your working directory?

No, but you need to provide the correct path (can be absolute or relative)

> [@HelgavonLichtenstein](#):
>
> Whether this is actually the correct format I don’t know. But it works.

That looks fine, though if you’re making a CSV I highly recommend putting stuff in a team or format then using CSV.jl

---

<div class="post-metadata">

### Author: ![HelgavonLichtenstein](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/helgavonlichtenstein/32/5005_2.png) [@HelgavonLichtenstein](https://discourse.julialang.org/u/HelgavonLichtenstein)
#### Post date: [January 8, 2020, 10:04am UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/6 "2020-01-08T10:04:49Z")

</div>

Thank you for your reply. Could you explain what you mean by team or format?

---

<div class="post-metadata">

### Author: ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)
#### Post date: [January 8, 2020, 11:27am UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/7 "2020-01-08T11:27:36Z")

</div>

For CSV files there is a package which you might find useful [Home · CSV.jl](https://juliadata.github.io/CSV.jl/stable/)

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [January 8, 2020, 3:01pm UTC](https://discourse.julialang.org/t/how-to-save-multiple-output-lines-to-a-txt-or-csv-file/33062/8 "2020-01-08T15:01:52Z")

</div>

> [@HelgavonLichtenstein](#):
>
> Could you explain what you mean by team or forma

Heh… phone typo. Should have been “Put stuff in a **Tables.jl** format then…”
