使用Python子进程处理通配符扩展

2024-03-29 00:26:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我调用这个函数并使用%s*silent来读取具有以下格式名称的文件:name.number.silent.

我的名字来自start_model.split('/')[-1].split('.')[0],所以不用担心。你知道吗

这显然不起作用,因为这些命令实际上从未传递给shell。如果我要使用glob,如何修改代码来执行下面的操作?你知道吗

from subprocess import call

def fragment_score(rosetta_path, silent_input_and_score_output, start_model):
    call([rosetta_path,
          '-mode score', 
          '-in::file::silent', '%s/%s*silent' % (silent_input_and_score_output, start_model.split('/')[-1].split('.')[0]),
          '-scorefile', '%s/scores1' % silent_input_and_score_output,
          '-n_matches', '50'])

Tags: and文件path函数名称inputoutputmodel
1条回答
网友
1楼 · 发布于 2024-03-29 00:26:37

使用the Python ^{} module生成一个glob结果列表,并将其拼接到参数列表中的同一位置,否则将使用一个shell用关联匹配列表替换glob表达式:

from subprocess import call
from glob import glob

def fragment_score(rosetta_path, silent_input_and_score_output, start_model):
    glob_exp = '%s/%s*silent' % (silent_input_and_score_output, start_model.split('/')[-1].split('.')[0])
    glob_results = glob(glob_exp)
    call([rosetta_path,
          '-mode score', 
          '-in::file::silent'
         ] + glob_results + [
          '-scorefile', '%s/scores1' % silent_input_and_score_output,
          '-n_matches', '50'])

在当前的Python3.x中,有一些语法使其更自然:

    call([rosetta_path,
          '-mode score', 
          '-in::file::silent',
          *glob_results,
          '-scorefile', '%s/scores1' % silent_input_and_score_output,
          '-n_matches', '50'])

相关问题 更多 >