【问题标题】:Run external program to each element of list对列表的每个元素运行外部程序
【发布时间】:2018-01-11 00:42:39
【问题描述】:

我正在尝试为分子列表(SMILES 格式)中的每个元素(分子)调用外部程序(Openbabel)。但是,我不断收到同样的错误:

/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)

【问题讨论】:

    标签: python subprocess call openbabel


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 2020-12-10
      • 2014-06-09
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      相关资源
      最近更新 更多