Save with an object value in file name

Hi guys:

I am using the package JLD to save objects. How to add the value of some parameters in the file name? For example, if I have a parameter a =0.5, I want the value of a to show up in the file name so I can remember what this file represents in the future. I tried the string format like in python but I failed. Thanks.

https://docs.julialang.org/en/v1/manual/strings/#string-interpolation

julia> a = 0.5
0.5

julia> filename = "saved_file_a=$a.jld"
"saved_file_a=0.5.jld"
1 Like

If you’re on Windows, you can serialize your parameters (or anything else) to an Extended Attributes Stream like this

using Serialization

params = Dict(:a=>5, :b=>7)

open("results.txt", "w") do io 
    println(io, "Here are my results")
end

open("results.txt:Strml:\$DATA", "w") do io 
    serialize(io, params)
end


open("results.txt", "r") do io 
    println(read(io, String))
end

open("results.txt:Strml:\$DATA", "r") do io 
    println(deserialize(io))
end

This code will output

Here are my results

Dict(:a => 5, :b => 7)

This Stream will stay with your file and be preserved by Windows tools such as copy.

dir /r reveals them in directory listings

$ dir /r results.txt
 10/02/2022  12:56                20 results.txt
                                 36 results.txt:Strml:$DATA

I can’t find an easy way to do that on Linux, without shelling out to setattr

So you could, of course, serialize to a separate file e.g. results.txt.meta

2 Likes

Make sure to read the docstring of Serialization.serialize if you consider saving your data that way, so you know about its limitations and can determine whether those are fine for your use case.

1 Like