# Adding more data to a JLD2 file

**URL:** https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323
**Category:** General Usage
**Tags:** jld2
**Created:** [August 5, 2020, 11:37am UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323 "2020-08-05T11:37:08Z")
**Posts on this page:** 6
**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: [August 5, 2020, 11:37am UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323/1 "2020-08-05T11:37:09Z")

</div>

I’m slow with these things, but I cannot get a `JLD2` file to open and save new information.

Using the instructions [here](https://github.com/JuliaIO/JLD2.jl#file-interface)

```julia
jldopen("example.jld2", "w") do file
    file["bigdata"]
end

```

Is `file` supposed to be the name of the file, or is it just supposed to be `file`?  
Is `"bigdata"` meant to be the name of the vector/array/thing you are saving?

I’ve tried it two different ways, but no file is created and nothing is saved

```julia
jldopen("test", "a+") do file
    one_foodweb_result2
end

f = jldopen("test.jld2", "a+")
write(f, "test", one_foodweb_result2)
close(f)

```

What is incorrect with my code? I am using Julia 1.5 in Atom.

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [August 5, 2020, 12:53pm UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323/2 "2020-08-05T12:53:17Z")

</div>

The following code does what you probably want:

```julia
using JLD2
mediumdata=randn(5)
@save "example.jld2" mediumdata # create a file with some data

jldopen("example.jld2", "a+") do file
    file["bigdata"] = randn(6) 
    file["smalldata"] = randn(4) # put your data here instead of random
end
# append data

mediumdata=nothing # reset

@load "example.jld2" bigdata mediumdata smalldata

@show bigdata
@show mediumdata
@show smalldata # here we are

```

---

<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: [August 5, 2020, 2:53pm UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323/3 "2020-08-05T14:53:15Z")

</div>

Thank you for your reply. Is it possible this may not work when it is within a function?

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [August 5, 2020, 3:07pm UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323/4 "2020-08-05T15:07:06Z")

</div>

```julia
using JLD2

function append_jld2(fname, varname, data)
   jldopen(fname, "a+") do file
     file[varname] = data
   end
end

mediumdata=randn(5)
@save "example.jld2" mediumdata # create file

append_jld2("example.jld2", "bigdata", randn(6))

mediumdata=nothing # reset

@load "example.jld2" bigdata mediumdata

@show bigdata
@show mediumdata # here we are

```

---

<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: [August 5, 2020, 3:41pm UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323/5 "2020-08-05T15:41:06Z")

</div>

Thank you. When I rerun code to add more data, I get

```julia
ArgumentError: a group or dataset named one_foodweb_result2 is already present within this group

```

The file is already made, but I don’t understand why it wouldn’t be able to add more data. This is happening within a function, so the data I want to add is not in the global scope. Perhaps the issue is with adding something to the same object that is saved in the file?

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [August 5, 2020, 3:51pm UTC](https://discourse.julialang.org/t/adding-more-data-to-a-jld2-file/44323/6 "2020-08-05T15:51:59Z")

</div>

> [@HelgavonLichtenstein](#):
>
> Perhaps the issue is with adding something to the same object that is saved in the file?

Yes, to my knowledge you can’t “add” anything to an already saved dataset.
