使用python子进程调用处理通配符扩展
我正在调用此函数,并使用使用python子进程调用处理通配符扩展,python,subprocess,glob,Python,Subprocess,Glob,我正在调用此函数,并使用%s*silent读取具有以下格式名称的文件:name.number.silent. 我从start\u model.split('/')[-1].split('.')[0]中获取名称,所以不用担心 这显然不起作用,因为这些命令实际上从未传递给shell。如果我使用glob,我如何修改我的代码来完成下面的工作 from subprocess import call def fragment_score(rosetta_path, silent_input_and_sco
%s*silent
读取具有以下格式名称的文件:name.number.silent.
我从start\u 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'])
用于生成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'])
在最近的Python3上,使用
*
将glob
结果解压缩到列表中可以简化工作。非常感谢。我实际上使用的是Python 3.5。这个语法“]+glob_results+[”对我或我的编辑来说似乎无效。它是做什么的?它对3.5版有效吗?为什么它看起来无效?]
关闭前面以]
开头的列表,[
启动一个新列表,+
将这些列表与glob()的结果一起添加
call。它一点也不奇怪。因此,这与您在[1,2,3]+[4,5,6]+[7,8,9]
中使用的语法完全相同,[4,5,6]
是glob()返回的
call….也就是说,在Python3.5中,您可以使用与l=[4,5,6];print([1,2,3,*l,7,8,9]
中相同的语法将*glob_results
扩展到列表中。