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.

julia> a = 0.5

julia> filename = "saved_file_a=$a.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")

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

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

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

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


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