# Can I write a DelimitedFile with a header?

**URL:** <https://discourse.julialang.org/t/can-i-write-a-delimitedfile-with-a-header/27270>\
**Category:** General Usage\
**Tags:** question, input-output, io, delimitedfiles\
**Created:** [August 7, 2019, 2:43pm UTC](https://discourse.julialang.org/t/can-i-write-a-delimitedfile-with-a-header/27270 "2019-08-07T14:43:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [August 7, 2019, 2:43pm UTC](https://discourse.julialang.org/t/can-i-write-a-delimitedfile-with-a-header/27270/1 "2019-08-07T14:43:28Z")

</div>

Hi,  
I would like to save a matrix in txt format (I know there are better alternatives, but this is a two-liner). I can do that with

```julia
using DelimitedFiles
A = [[1 2]; [3 4]]
writedlm("./data.txt", A)

```

Can I also write a header in the file using `writedlm`? For example, the file would read

```julia
# This is my matrix A
1 2
3 4

```

Thanks

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [August 7, 2019, 2:54pm UTC](https://discourse.julialang.org/t/can-i-write-a-delimitedfile-with-a-header/27270/2 "2019-08-07T14:54:12Z")

</div>

Sure:

```julia
julia> using DelimitedFiles

julia> a = [[1 2]; [3 4]]
2×2 Array{Int64,2}:
 1 2
 3 4

julia> open("data.txt"; write=true) do f
         write(f, "# This is my matrix A\n")
         writedlm(f, a)
       end

shell> cat data.txt
# This is my matrix A
1 2
3 4

```

---

<div class="post-metadata">

**Author:** ![alvrg](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@alvrg](https://discourse.julialang.org/u/alvrg)\
**Post date:** [August 7, 2019, 3:04pm UTC](https://discourse.julialang.org/t/can-i-write-a-delimitedfile-with-a-header/27270/3 "2019-08-07T15:04:41Z")

</div>

Great! Just what I wanted, thanks! 😃
