After researching many sources such as chemistry and github and stackoverflow, I have not found a way to add codes to the exist script in order to redirect pybel warnings to a log file.
However, I can provide you a possible solution here by using script. By modifying your code:
import pybel
smiles = pybel.readfile('smi', 'smiles.txt')
converted = []
for mol in smiles:
print(mol)
smiles_str = mol.write('smiles')
inchikey_str = mol.write('inchikey')
with a command run:
script -c "python smiles2sdf.py" log.txt
It will output everything shown on terminal during executing to log.txt. This feature makes us be careful to choose what will be appeared on screen.
Further information about this package is here.
Hope this help.
p.s: I also want to capture pybel warnings programmatically. If there is a possible method to do that, please provide me. Thank you very much.