在 python 中执行与不同软件相关的命令的语句是什么?

Var*_*ani 8 python

我正在使用 Unix 操作系统,我想知道如何在 Python 脚本中执行可从不同软件执行的命令。我在我的系统中安装了这个名为 HAL Tools 的软件,它有一个名为 maf2hal 的命令,带有两个参数,分别是输入和输出文件。命令的路径保存在我的 .bash_profile 中的 PATH 变量下。我同样从 UNIX 调用命令:

[root ~]$ maf2hal inputfile outputfile
Run Code Online (Sandbox Code Playgroud)

我想从 Python 脚本中调用此命令。我需要使用的语句或函数是什么。

Pau*_* Lo 3

1.使用os.popen()执行命令:

import os
os.popen("maf2hal inputfile outputfile")
Run Code Online (Sandbox Code Playgroud)

2.子进程.call()

from subprocess import call
call("maf2hal inputfile outputfile", shell=True)
Run Code Online (Sandbox Code Playgroud)

  • 是的,我检查了之前的讨论。看来 subprocess 确实更适合。谢谢! (2认同)