python调用脚本或shell有下面三种方式:

os.system()
特点:
(1)可以调用脚本。
(2)可以判断是否正确执行。
(3)满足不了标准输出 && 错误

commands模块
特点:
(1). commands.getstatusoutput(cmd)
用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result). cmd执行的方式是{ cmd ; } 2&get;&1, 这样返回结果里面就会包含标准输出和标准错误.
(2). commands.getoutput(cmd)
只返回执行的结果, 忽略返回值.
(3). commands.getstatus(file)
返回ls -ld file执行的结果.

subprocess模块
推荐使用这种方法

import subprocess
  def create_process(cmd):
  p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  result = p.stdout.read()
  code = p.wait()
  return code, result
print create_process('df -h')

 

相关文章:

  • 2021-11-20
  • 2021-11-20
  • 2021-09-04
  • 2021-06-02
  • 2022-02-06
  • 2021-12-04
  • 2021-10-21
猜你喜欢
  • 2021-11-20
  • 2021-10-05
  • 2021-10-18
  • 2021-12-16
  • 2021-12-27
  • 2022-12-23
相关资源
相似解决方案