# Printing an array into a text file

**URL:** <https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101>\
**Category:** New to Julia\
**Tags:** array\
**Created:** [August 26, 2018, 11:22pm UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101 "2018-08-26T23:22:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [August 26, 2018, 11:22pm UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/1 "2018-08-26T23:22:05Z")

</div>

Hello,  
I have an array that I’d like to print into a text file.

```julia
notes = ["C4", "D4", "E4", "F4"]
outfile = "notes.txt"
f = open(outfile, "w")

for i in notes
	println(f, notes[i])
end

```

For some reason, my ‘for loop’ is complaining and saying “ArgumentError: invalid index: C4”

Thanks!  
Nakul

---

<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:** [August 26, 2018, 11:40pm UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/2 "2018-08-26T23:40:01Z")

</div>

Welcome!

First, check out [how to quote your code with backticks](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530) to maintain readability.

The issue is that your iterator `for i in notes` means that each `i` is the element of `notes`. So what you want is

```julia
for i in notes # or for note in notes
println(f, i)
end

```

Or if you want to iterate through the indices of `notes`, you can use the function `eachindex`

```julia
for i in eachindex(notes)
println(f, notes[i])
end

```

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [August 26, 2018, 11:58pm UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/4 "2018-08-26T23:58:27Z")

</div>

Thank you! For some reason the array isn’t getting stored into the text file. Any idea why?

---

<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:** [August 27, 2018, 12:04am UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/5 "2018-08-27T00:04:28Z")

</div>

are you closing the text file when you are done?

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [August 27, 2018, 12:11am UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/6 "2018-08-27T00:11:11Z")

</div>

I do close it but it is always empty, even when I open it.

---

<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:** [August 27, 2018, 12:42am UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/7 "2018-08-27T00:42:32Z")

</div>

If you could provide a complete minimum working example it would help us debug your problem a bit more

---

<div class="post-metadata">

**Author:** ![Nakul\_Tiruviluamala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nakul_tiruviluamala/32/11228_2.png) [@Nakul\_Tiruviluamala](https://discourse.julialang.org/u/Nakul_Tiruviluamala)\
**Post date:** [August 27, 2018, 1:24am UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/8 "2018-08-27T01:24:52Z")

</div>

I finally understood what you meant by closing the file. I thought you meant in my editor haha, but close(f) worked for me. Thanks so much. That was a close call…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 27, 2018, 2:13am UTC](https://discourse.julialang.org/t/printing-an-array-into-a-text-file/14101/9 "2018-08-27T02:13:19Z")

</div>

You can use a `do-block` to conveniently ensure that your file gets closed when you’re done writing to it:

```julia
notes = ["C4", "D4", "E4", "F4"]
outfile = "notes.txt"
open(outfile, "w") do f
  for i in notes
    println(f, i)
  end
end # the file f is automatically closed after this block finishes

```

To learn about why this works, check out the manual: [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#Do-Block-Syntax-for-Function-Arguments-1)
