# Save variable

**URL:** <https://discourse.julialang.org/t/save-variable/33986>\
**Category:** New to Julia\
**Created:** [January 30, 2020, 3:59pm UTC](https://discourse.julialang.org/t/save-variable/33986 "2020-01-30T15:59:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [January 30, 2020, 3:59pm UTC](https://discourse.julialang.org/t/save-variable/33986/1 "2020-01-30T15:59:39Z")

</div>

Hi everyone,  
Sorry for the elementary question.  
How do I save any variables after a program runs in Julia?  
Could someone give me an example of how to save the variable “a” in a file .dat in this code?

global a  
for i in 1:10  
a = a+i  
end

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [January 30, 2020, 4:19pm UTC](https://discourse.julialang.org/t/save-variable/33986/2 "2020-01-30T16:19:01Z")

</div>

There’s a few options but JLD2 works fine for me :

```julia
using FileIO, JLD2
a = 1
FileIO.save("myfile.jld2","a",a)
b = FileIO.load("myfile.jld2","a")

```

---

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [January 30, 2020, 4:19pm UTC](https://discourse.julialang.org/t/save-variable/33986/3 "2020-01-30T16:19:52Z")

</div>

You can also use `CSV` or `DelimitedFiles` if you just want to save data in a text file.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [January 30, 2020, 4:27pm UTC](https://discourse.julialang.org/t/save-variable/33986/4 "2020-01-30T16:27:49Z")

</div>

The built-in serializer can be quite nice to use in some cases.

```julia
using Serialization

a = randn(100,100)
f = serialize("file1.dat", a)

a′ = deserialize("file1.dat")

println(a == a′)

```

Note however that the built-in serializer is designed for inter-process communication (IPC) and not really for stashing data on disk. Therefore, serialized data output by one version of Julia is not guaranteed to be readable by other versions of Julia, so this is most definitely not for long-term storage. It can however still be extremely useful if you are only looking for short-term storage of things which are reproducible. Last I checked, the serializer was quite fast.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [January 30, 2020, 6:05pm UTC](https://discourse.julialang.org/t/save-variable/33986/5 "2020-01-30T18:05:50Z")

</div>

> [@angeloaliano1](#):
>
> Could someone give me an example of how to save the variable “a” in a file .dat in this code?
> 
> global a  
> for i in 1:10  
> a = a+i  
> end

Also, this doesn’t do what you think it does and wouldn’t work. My suggestion is to stay in jupyter notebooks as you are learning, and just drop that global. That is, use

```julia
a = 0
for i in 1:10
   a = a+i
end

```

If you want to do this in a file or in Atom/Juno, then put a function around it.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 9, 2023, 2:12pm UTC](https://discourse.julialang.org/t/save-variable/33986/6 "2023-05-09T14:12:27Z")

</div>

A post was split to a new topic: [Saving to encrypted files?](https://discourse.julialang.org/t/saving-to-encrypted-files/98555)
