# Writing file inside a loop

**URL:** <https://discourse.julialang.org/t/writing-file-inside-a-loop/57376>\
**Category:** Data\
**Created:** [March 17, 2021, 12:19pm UTC](https://discourse.julialang.org/t/writing-file-inside-a-loop/57376 "2021-03-17T12:19:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccmejia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccmejia/32/18680_2.png) [@ccmejia](https://discourse.julialang.org/u/ccmejia)\
**Post date:** [March 17, 2021, 12:19pm UTC](https://discourse.julialang.org/t/writing-file-inside-a-loop/57376/1 "2021-03-17T12:19:18Z")

</div>

Hi there!  
I’m running some stuff in a cluster… Due to this take long time calculations I would like to follow some kind of output file to know how it is going… It seems that inside a loop the output file is not modified step by step, on the contrary the entire file is written at the end of the loop.

Here an basic example:

f = open(“geek.txt”, “w”)  
for i=1:10  
sleep(3)  
println(f, “$i\n”)  
end  
close(f)

Any idea how I can follow the execution in long time calculations?

Thanks in advance!  
Best,  
Crist.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 17, 2021, 12:24pm UTC](https://discourse.julialang.org/t/writing-file-inside-a-loop/57376/2 "2021-03-17T12:24:08Z")

</div>

The stream is buffered so you need to call `flush(f)` if you want the bytes to be written directly.

```julia
open("f.txt", "w") do f
    for i = 1:10
        sleep(3)
        println(f, i)
        flush(f)
    end
end

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 17, 2021, 12:28pm UTC](https://discourse.julialang.org/t/writing-file-inside-a-loop/57376/3 "2021-03-17T12:28:03Z")

</div>

You should `flush` it from time to time.

As a side note, is better to use logging system to do this kind of stuff, then you’ll be able to modify your output and turn on and off parts of output, without modifying the code.

[LoggingExtras.jl](https://github.com/oxinabox/LoggingExtras.jl) should be really helpful.

---

<div class="post-metadata">

**Author:** ![ccmejia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccmejia/32/18680_2.png) [@ccmejia](https://discourse.julialang.org/u/ccmejia)\
**Post date:** [March 17, 2021, 1:17pm UTC](https://discourse.julialang.org/t/writing-file-inside-a-loop/57376/4 "2021-03-17T13:17:24Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![ccmejia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccmejia/32/18680_2.png) [@ccmejia](https://discourse.julialang.org/u/ccmejia)\
**Post date:** [March 17, 2021, 1:18pm UTC](https://discourse.julialang.org/t/writing-file-inside-a-loop/57376/5 "2021-03-17T13:18:15Z")

</div>

Thank you! I will take a look to this package.
