python调用命令行

python调用CLI

import logging
from subprocess import Popen, PIPE


# CLI: Command Line Interface
def runCLI():
# __cmd_link = self.cur_dir + "\\stlink_cli\\ST-LINK_CLI.exe -c SWD SWJCLK=5 UR"
__cmd_link = "dir"

# 多用 shell=True. shell=False只能运行bat文件或直接在终端中执行. 此例中显示的内容也会有区别
_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