From 07657b64005dce5e53801ab6199388de56e52e19 Mon Sep 17 00:00:00 2001 From: Daniele Rapetti <5535617+Iximiel@users.noreply.github.com> Date: Mon, 2 Feb 2026 08:56:59 +0100 Subject: [PATCH] _guessplumedroot now deletes the temporary file used to store the plumed output In the mkstemp documentation there is the following sentence: > Unlike TemporaryFile(), the user of mkstemp() is responsible for > deleting the temporary file when done with it. So I decided to use the higher level routine to avoid putting a new file in the tmp dir every time I run some test (or the user tries to create a new input with the InputBuilder) --- python/plumed.pyx | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/python/plumed.pyx b/python/plumed.pyx index 60626b897b..90956df1e2 100644 --- a/python/plumed.pyx +++ b/python/plumed.pyx @@ -708,21 +708,21 @@ def _guessplumedroot(kernel=None): dir from there. """ try: - import tempfile - log=tempfile.mkstemp()[1] - with Plumed(kernel) as p: - p.cmd("setLogFile",log) - p.cmd("init") - i=0 - root="" - with open(log) as fin: - for line in fin: - i=i+1 - if re.match("PLUMED: Root: ",line): - root=re.sub("PLUMED: Root: ","",line).rstrip("\n") - break - if len(root)>0: - return root + from tempfile import NamedTemporaryFile + #mkstemp does not delete the created file, so we improvise: + with NamedTemporaryFile() as tmpfile: + log=tmpfile.name + with Plumed(kernel) as p: + p.cmd("setLogFile",log) + p.cmd("init") + root=None + with open(log) as fin: + for line in fin: + if re.match("PLUMED: Root: ",line): + root=re.sub("PLUMED: Root: ","",line).rstrip("\n") + break + if root and len(root)>0: + return root except: pass # alternative solution, search for a plumed executable in the path