0%
python调用CLI
- CLI = Command Line Interface, 即命令行
- 方法有很多, 通用性和安全性最好的就是
subprocess.Popen
这个方法. 见文档
- example:
import logging from subprocess import Popen, PIPE
def runCLI(): __cmd_link = "dir"
_pp = Popen(__cmd_link, shell=True, stdout=PIPE, stderr=PIPE) out, err = _pp.communicate()
logging.debug("cmd > " + __cmd_link) logging.debug("out > " + out.rstrip()) logging.debug("err > " + err.rstrip()) return _pp.returncode
if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG) logging.debug(runCLI())
|
原创于 DRA&PHO