Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/openfermion/chem/molecular_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""Class and functions to store quantum chemistry data."""

import os
import uuid
import tempfile
import shutil
import numpy
import h5py
Expand Down Expand Up @@ -703,8 +703,12 @@ def save(self):
"""Method to save the class under a systematic name."""
# Create a temporary file and swap it to the original name in case
# data needs to be loaded while saving
tmp_name = uuid.uuid4()
with h5py.File("{}.hdf5".format(tmp_name), "w") as f:
with tempfile.NamedTemporaryFile(
delete=False, suffix='.hdf5', dir=os.path.dirname(os.path.abspath(self.filename))
) as tmp_file:
tmp_name = tmp_file.name
Comment thread
mhucka marked this conversation as resolved.
Outdated

with h5py.File(tmp_name, "w") as f:
# Save geometry (atoms and positions need to be separate):
d_geom = f.create_group("geometry")
if not isinstance(self.geometry, basestring):
Expand Down Expand Up @@ -844,7 +848,7 @@ def save(self):
# Nothing to do but carry on.
pass

shutil.move("{}.hdf5".format(tmp_name), "{}.hdf5".format(self.filename))
shutil.move(tmp_name, "{}.hdf5".format(self.filename))

def load(self):
geometry = []
Expand Down
Loading