This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

overwrite in HDF5 format

0 votes

I've noticed the HDF5File command only has options to write and read. Is there a way to overwrite?

I have the following code.

fileName = "test1.h5"

steps = 5

hdf = HDF5File(mesh.mpi_comm(), fileName, "w")#mesh defined elsewhere
    hdf.write(mesh, "mesh")
    for i in range(steps):
        TimeStep() #function that takes a step in time
        hdf.write(fr_1,"frsol",i) #frsol is the solution from the TimeStep function

I'd like to modify this code so that if I run it again it deletes what was there before and writes new solutions. That way I don't have to look up the file, delete it, and then run the code.

asked Jul 25, 2016 by aldenpack FEniCS User (1,450 points)
edited Jul 26, 2016 by aldenpack

Do you mean append instead of overwrite?

I've edited the question to better explain what I'm trying to do. I'd like to write over the previous solution, not add more solutions to the file (append).

1 Answer

0 votes
 
Best answer

I'd like to modify this code so that if I run it again it deletes what was there before and writes new solutions. That way I don't have to look up the file, delete it, and then run the code.

Can you just delete the .h5 file whenever you run the script? Like this:

import os
fileName = "test1.h5"
try:
    os.remove(fileName)
except OSError:
    pass
# start doing things...
hdf = HDF5File(mesh.mpi_comm(), fileName, "w")

If you are running in parallel you may need to add an if statement to restrict the deletion to a single process and an MPI barrier to avoid a race condition, but unless I'm misunderstanding your question, this should basically do what you want.

answered Jul 27, 2016 by FF FEniCS User (4,630 points)
selected Jul 27, 2016 by aldenpack
...