Ok Paulo,

with open(tempfn, 'rb') as reader:
    fsuppl = Chem.ForwardSDMolSupplier(reader)
    for m2 in fsuppl:
        print(m2.GetProp('_Name'))

works fine for what I want to do.

Best,

Jean-Marc


Le 29/03/2020 à 16:35, Jean-Marc Nuzillard a écrit :
Hi Paolo,

neither tempfn (a string)  nor reader (a SDMolSupplier) have a close() method.

I am not sure about what you wrote about the "with" statement.

Attempting:
with Chem.SDMolSupplier(tempfn) as reader:
    m2 = reader[0]
    print(m2.GetProp('_Name'))

resulted in:
    with Chem.SDMolSupplier(tempfn) as reader:
AttributeError: __enter__

Best,

Jean-Marc


Le 29/03/2020 à 15:51, Paolo Tosco a écrit :
Hi Jean-Marc,

tempfn.close() should allow you to delete the file.
A better alternative would be using using

with temp file.TemporaryFile as tempfn
     ...

As the file will be automatically closed and deleted for you as soon as it goes out of scope at the end of the context manager block.

Cheers,
p.

On 29 Mar 2020, at 13:44, Jean-Marc Nuzillard <jm.nuzill...@univ-reims.fr> wrote:

Dear all,

The following code:
______________________________________
from rdkit import Chem
import os
import tempfile

# create temp .sdf file with a benzene molecule inside
m1 = Chem.MolFromSmiles('c1ccccc1')
m1.SetProp('_Name', 'benzene')
tempfn = tempfile.mktemp('.sdf')
writer = Chem.SDWriter(tempfn)
writer.write(m1)
writer.close()

# read the molecule from temp .sdf file and print compound name
reader = Chem.SDMolSupplier(tempfn)
m2 = reader[0]
print(m2.GetProp('_Name'))

# unlik temp file
os.unlink(tempfn)
______________________________________

running it in windows gives:

benzene
Traceback (most recent call last):
   File "rdclose.py", line 21, in <module>
     os.unlink(tempfn)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
'C:\\Users\\jmn\\AppData\\Local\\Temp\\tmpojn1j4nb.sdf'

I suspect the reason is that reader file is not closed.
Trying reader.close() results in a message saying that reader has no close() method.

Thank you in advance for helping me to get rid of the temp file.

Take care,

Jean-Marc Nuzillard

--
Jean-Marc Nuzillard
Directeur de Recherches au CNRS

Institut de Chimie Moléculaire de Reims
CNRS UMR 7312
Moulin de la Housse
CPCBAI, Bâtiment 18
BP 1039
51687 REIMS Cedex 2
France

Tel : 03 26 91 82 10
Fax : 03 26 91 31 66
http://www.univ-reims.fr/icmr
http://eos.univ-reims.fr/LSD/CSNteam.html

http://www.univ-reims.fr/LSD/
http://www.univ-reims.fr/LSD/JmnSoft/



_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss




--
Jean-Marc Nuzillard
Directeur de Recherches au CNRS

Institut de Chimie Moléculaire de Reims
CNRS UMR 7312
Moulin de la Housse
CPCBAI, Bâtiment 18
BP 1039
51687 REIMS Cedex 2
France

Tel : 03 26 91 82 10
Fax : 03 26 91 31 66
http://www.univ-reims.fr/icmr
http://eos.univ-reims.fr/LSD/CSNteam.html

http://www.univ-reims.fr/LSD/
http://www.univ-reims.fr/LSD/JmnSoft/



_______________________________________________
Rdkit-discuss mailing list
Rdkit-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rdkit-discuss

Reply via email to