# Basic writing to file question(s)

**URL:** <https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 11, 2021, 6:51pm UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757 "2021-06-11T18:51:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)\
**Post date:** [June 11, 2021, 6:51pm UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757/1 "2021-06-11T18:51:37Z")

</div>

Hello! I’m still pretty new. I’ve checked the documentation on this but am not getting the full picture yet.

Here’s my situation: I’m doing some benchmarking and want to test my program on different inputs one at a time. Each time I run the program, I want to save some information to a markdown file. Then I want to change the inputs, run the program again, and append the new information to the same markdown file.

I know that to make a file I use the command touch. Question: I am in a working directory, .atom and have been unable to change this working directory by using cd in the the REPL. Can I have touch() make a file in a different directory? For example, I have a subdirectory in .atom where I am working on this project and I would like to save it here.

Next, I am also having problems appending my file. I created a file in the REPL and am opening it in append mode in my program using open(). I am able to write successfully to the file using write. However, when I run the program again, it overwrites my file. Based on the documentation, it seems like I should be using write!() which is in the LibGit2 library, but the function isn’t recognized when I call it. LibGit2 is installed in my home directory so it should be working. Will write!() work as I think it will and is it depricated? How should I append the file otherwise?

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 11, 2021, 7:00pm UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757/2 "2021-06-11T19:00:15Z")

</div>

You should really post a minimal, running example illustrating your point. You know, a few lines of code say more than a thousand words.

[https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you)

---

<div class="post-metadata">

**Author:** ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)\
**Post date:** [June 11, 2021, 7:02pm UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757/3 "2021-06-11T19:02:15Z")

</div>

open(“comparison”,“a”)  
write(“comparison.md”, "# " , gallery, string(x0), " to ", string(x1), " degree ", string(n))  
close

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [June 11, 2021, 7:06pm UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757/4 "2021-06-11T19:06:54Z")

</div>

Well, hardly a MWE… please do read the link I sent. But try something like

```julia
  julia> io = open("myfile.txt", "a");                                                                                                                                                          
                                                                                                                                                                                                
  julia> write(io, "Hello world!");                                                                                                                                                             
                                                                                                                                                                                                
  julia> close(io); 

```

(from the doc-string) Note the `io`. In your example you open a file with `open` and then open another one with `write`.

---

<div class="post-metadata">

**Author:** ![Brian\_Becsi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian_becsi/32/19925_2.png) [@Brian\_Becsi](https://discourse.julialang.org/u/Brian_Becsi)\
**Post date:** [June 11, 2021, 7:10pm UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757/5 "2021-06-11T19:10:27Z")

</div>

very helpful and appreciated!

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 13, 2021, 6:09am UTC](https://discourse.julialang.org/t/basic-writing-to-file-question-s/62757/6 "2021-06-13T06:09:02Z")

</div>

To help you not forget/fail to close the stream, you can use

```julia
open(filename, "a") do io
    write(io, contents)
end

```
