python - 使用 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'])
解决方案
使用Pythonglob
模块生成 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'])
在当前的 Python 3.x 中,有一些语法使它更自然:
call([rosetta_path,
'-mode score',
'-in::file::silent',
*glob_results,
'-scorefile', '%s/scores1' % silent_input_and_score_output,
'-n_matches', '50'])
推荐阅读
- python - tf.data.Dataset + tf.lookup.index_table_from_file 导致“表未初始化”错误
- html - 与 Wordpress 中的自定义 CSS 和重力形式对齐
- android - 具有不同本地和远程行为的存储库模式
- python - 使用用户定义函数时如何让两个函数返回?
- asp.net-core - ASP.NET Core 如何在第一次运行时初始化连接字符串?
- marklogic - 如何将自定义角色从数据库 1 复制到数据库 2
- django - Django:无法正确更新数据库
- sql-server - 如何使用 T SQL 删除维护计划?
- doxygen - Doxygen:具有相同名称的参数和方法
- jenkins - 通过种子作业创建后立即启动詹金斯作业,带参数?