Skip to content

Commit 0cb85a6

Browse files
authored
Merge pull request #1366 from Iximiel/fix29/pythonInterfaceTmpFile
_guessplumedroot now deletes the temporary file used to store the plumed output
2 parents 4095009 + 07657b6 commit 0cb85a6

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

python/plumed.pyx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -708,21 +708,21 @@ def _guessplumedroot(kernel=None):
708708
dir from there.
709709
"""
710710
try:
711-
import tempfile
712-
log=tempfile.mkstemp()[1]
713-
with Plumed(kernel) as p:
714-
p.cmd("setLogFile",log)
715-
p.cmd("init")
716-
i=0
717-
root=""
718-
with open(log) as fin:
719-
for line in fin:
720-
i=i+1
721-
if re.match("PLUMED: Root: ",line):
722-
root=re.sub("PLUMED: Root: ","",line).rstrip("\n")
723-
break
724-
if len(root)>0:
725-
return root
711+
from tempfile import NamedTemporaryFile
712+
#mkstemp does not delete the created file, so we improvise:
713+
with NamedTemporaryFile() as tmpfile:
714+
log=tmpfile.name
715+
with Plumed(kernel) as p:
716+
p.cmd("setLogFile",log)
717+
p.cmd("init")
718+
root=None
719+
with open(log) as fin:
720+
for line in fin:
721+
if re.match("PLUMED: Root: ",line):
722+
root=re.sub("PLUMED: Root: ","",line).rstrip("\n")
723+
break
724+
if root and len(root)>0:
725+
return root
726726
except:
727727
pass
728728
# alternative solution, search for a plumed executable in the path

0 commit comments

Comments
 (0)