对列表中的每个元素运行外部程序
问题描述:
我试图调用外部程序(Openbabel)给分子列表(SMILES格式)中的每个元素(分子)。然而,我一直得到相同的错误:对列表中的每个元素运行外部程序
/bin/sh: 1: Syntax error: "(" unexpected (expecting ")").
我的代码有什么问题?
from subprocess import call
with open('test_zinc.smi') as f:
smiles = [(line.split())[0] for line in f]
def call_obabel(smi):
for mol in smi:
call('(obabel %s -otxt -s %s -at %s -aa)' % ('fda_approved.fs', mol, '5'), shell=True)
call_obabel(smiles)
答
subprocess.call
需要可迭代的命令和参数。如果您需要将命令行参数传递给进程,则它们属于可迭代的。也不建议使用shell=True
,因为这可能会造成安全隐患。我会在下面省略它。
试试这个:
def call_obabel(smi):
for mol in smi:
cmd = ('obabel', 'fda_approved.fs', '-otxt', '-s', mol, '-at', '5', '-aa')
call(cmd)