# How can I read the names of variables/object stored/saved/written in a file using HDF5 in Julia

**URL:** https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204
**Category:** Data
**Created:** [July 17, 2020, 2:54am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204 "2020-07-17T02:54:51Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)
#### Post date: [July 17, 2020, 2:54am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/1 "2020-07-17T02:54:51Z")

</div>

Hi guys, I hope you all have been safe from this covid19 virus.

I have built a code that runs different experiments and automatically build folders for those experiments and inside them subfolders for for different parameters that I have used for the same experiment. Inside these subfolders I save .h5 file with the result of the experiments that could be one or more variables.

Now I would like to build a function that is able to read the variables saved in these files, either by providing the full path name and the specific name of variable I am interested to read or without providing the name of the specific variable (in this case read all of them).

I am using as initial pattern a code that some other guy was using to read attributed or something, please see following:

```julia
  function readManyFilesHDF5(homePath::String, groupName::String, N::Int64)
    original_path_name = pwd() # get the actual path name
    @show original_path_name

    roots = homePath # get the full path to the folder which contains the files under consideration
    files = readdir(roots) # read the file names of the files in the folder under consideration

    use_file = copy(files) # create a copy of the list of the file names, and use it later to remove the file names which have been read already
    for file_name in files # get each file name, in the folder under consideration, in sequence
	       fname = joinpath(roots,file_name) # creates the full path to the file in consideration
	       h5open(fname,"r") do fid # this is the 'fid' used in 'readattr' function as the first argument
		        global use_file # the original copy of the list of file names in the folder under consideration
                variableNames = names(fid) # get the names of variables in the file under consideration
                data = [fid[joinpath(key, "rest/of/path")] for key in variableNames] # get the data stored in the variables written in file under consideration

	       end
     end

     cd(original_path_name) # restore the original path name
     @show pwd()

     println(length(use_file))
end

```

but I am not able to read the names of the variables/objects saved/written in those files, please refer to part of the code:

```julia
variableNames = names(fid) # get the names of variables in the file under consideration

```

I thank you very much in advance for your time and support. Thank you very much also for your understanding.

Cheers

Ergnoor

---

<div class="post-metadata">

### Author: ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)
#### Post date: [July 17, 2020, 5:24am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/2 "2020-07-17T05:24:52Z")

</div>

Not answering your question, but this might be of interest:

> **[GitHub - JuliaDynamics/DrWatson.jl: The perfect sidekick to your scientific...](https://github.com/JuliaDynamics/DrWatson.jl)**
>
> The perfect sidekick to your scientific inquiries. Contribute to JuliaDynamics/DrWatson.jl development by creating an account on GitHub.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 17, 2020, 2:42pm UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/3 "2020-07-17T14:42:38Z")

</div>

`names` reads the variable names in a group. So you need to know which group you wrote the variables to. See the [docs](https://github.com/JuliaIO/HDF5.jl/blob/master/doc/hdf5.md#getting-information).

If you do

```julia
g = fid["mygroup"]
names(g)

```

You get the names for that group.

One bigger picture question is: why use HDF5 directly rather than a wrapper like JLD?

---

<div class="post-metadata">

### Author: ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)
#### Post date: [July 20, 2020, 1:48am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/4 "2020-07-20T01:48:22Z")

</div>

thank you very much Lutz for the answer and Greg for the suggestion, I appreciate it very much.

Cheers.

Ergnor

---

<div class="post-metadata">

### Author: ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)
#### Post date: [July 20, 2020, 2:47am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/5 "2020-07-20T02:47:02Z")

</div>

@hendri54 Hi Lutz, I tried your solution as follows:

```julia
function readManyFilesHDF5(homePath::String)
    original_path_name = pwd()
    @show original_path_name

    roots = homePath
    @show roots
    files = readdir(roots)
    @show files
    for file_name in files
        @show file_name
	    h5open(file_name,"r") do fid
            groupNames = names(fid)
            @show groupNames
            for keyGroup in groupNames
                @show keyGroup
                @show fid
                g = fid[keyGroup]
                @show g
                variableNames = names(g)
                # variableNames = names(keyGroup)
                @show variableNames
                for keyVariable in variableNames
                    @show typeof(h5read(file_name, keyGroup))
                    global data = h5read(file_name, keyGroup)
                end
            end
	    end
     end

     cd(original_path_name)
     @show pwd()
     return data
end

```

but at:

```julia
 variableNames = names(g)

```

it gives me this error:

```julia
ERROR: MethodError: no method matching names(::HDF5Dataset)

```

before the error the printing on REPL is as follows:

```julia
original_path_name = "/home/user/.julia/dev/PreImageProblems/experiments/Euclidean/_angles"
roots = "/home/user/.julia/dev/PreImageProblems/experiments/Euclidean/_angles"
files = ["mydata.h5", "mynewData.h5"]
file_name = "mydata.h5"
groupNames = ["myGroupPSNR", "myGroupSSIM", "myGroupXq"]
keyGroup = "myGroupPSNR"
fid = HDF5 data file: mydata.h5
g = HDF5 dataset: /myGroupPSNR (file: mydata.h5 xfer_mode: 0)

```

Thank you again for your time and support.

Cheers

Ergnoor

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 20, 2020, 1:01pm UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/6 "2020-07-20T13:01:58Z")

</div>

I’m afraid this is now above my paygrade.

It looks like the objects in `groupNames` are not groups by datasets.

Suppose you try

```julia
obj = fid["myGroupPSNR"]
data = read(obj)

```

That might help to identify what `myGroupPSNR` actually points to. At this point, it would be helpful to have the input of someone who has a lot better understanding of HDF5, though.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [July 20, 2020, 1:08pm UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/7 "2020-07-20T13:08:28Z")

</div>

Also: are you tied to the HDF5 format or could you use a wrapper, such as JLD, which is better documented and hides the internals more?

---

<div class="post-metadata">

### Author: ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)
#### Post date: [July 21, 2020, 7:52am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/8 "2020-07-21T07:52:20Z")

</div>

@musm Hi Mustafa, sorry to disturb you but I saw you are quite active recently with contributions to HDF5.jl

My question is how to get the names of the variables saved/written in a group of a .h5 file?

Thank you very much for your time and support.

Cheers

Ergnoor

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [July 21, 2020, 1:07pm UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/9 "2020-07-21T13:07:23Z")

</div>

As a bandaid solution, did you consider writing just one object by file, and this object is a NamedTuple or Dict that has the names of the variables as the keys? So the variables names are inside the object itself?

---

<div class="post-metadata">

### Author: ![Ujku\_KU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ujku_ku/32/13159_2.png) [@Ujku\_KU](https://discourse.julialang.org/u/Ujku_KU)
#### Post date: [July 22, 2020, 2:20am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/10 "2020-07-22T02:20:00Z")

</div>

@Henrique_Becker Hi Henrique and thank you for your bandaid solution (I did consider it myself), I appreciate it very much, but in my case I already have a solution that singles out a group as follows:

```julia
function readManyFilesHDF5(homePath::String, fileName::String, groupName::String) # this is kind of final
    original_path_name = pwd()

    roots = homePath
    checkExistFile = false::Bool
    checkExistGroup = false::Bool

    files = readdir(roots)

    data = Dict{String,Any}()
    if length(filter(x->occursin(fileName,x),files))>=1

	    h5open(fileName,"r") do fid
            groupNames = names(fid)

            if length(filter(x->occursin(groupName,x),groupNames))>=1

                push!(data, groupName => h5read(fileName, groupName))
                checkExistGroup = true::Bool

            end
	    end
        checkExistFile = true::Bool
     end

     cd(original_path_name)

     return checkExistFile, checkExistGroup, data
end

```

and within this frame I can write only one variable for each group and use the same name either for the group and for the corresponding variable. Once I single out the group automatically I can single out even the variable. But this as you have said is just another bandaid solution 🙂.

Thank you very much for your time and support.

Cheers.

Ergnoor

---

<div class="post-metadata">

### Author: ![miromarszal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miromarszal/32/6721_2.png) [@miromarszal](https://discourse.julialang.org/u/miromarszal)
#### Post date: [April 15, 2021, 10:32am UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/11 "2021-04-15T10:32:27Z")

</div>

This post pops up when searching for the issue I had, so I thought I’ll make a quick note here for others. I tried running some older code and had this error

```julia
MethodError: no method matching names(::HDF5.Group)

```

It turns out that at [some point](https://github.com/JuliaIO/HDF5.jl/commit/f01ad317787540426d2ae910c4fdb7ee26662dfe) `names` was renamed to `keys`.

---

<div class="post-metadata">

### Author: ![leka0024](https://avatars.discourse-cdn.com/v4/letter/l/aeb1de/32.png) [@leka0024](https://discourse.julialang.org/u/leka0024)
#### Post date: [April 3, 2024, 3:55pm UTC](https://discourse.julialang.org/t/how-can-i-read-the-names-of-variables-object-stored-saved-written-in-a-file-using-hdf5-in-julia/43204/12 "2024-04-03T15:55:40Z")

</div>

@miromarszal Thank you!! Brand new to Julia, just trying to run a code project that someone put together from a while ago and was getting the `no matching method` for `names`. Switched to `keys` and working!
