开发者

Python调用系统命令的四种方法详解(os.system、os.popen、commands、subprocess)

开发者 https://www.devze.com 2023-03-30 09:27 出处:网络 作者: 风家一良
目录一、os.system方法二、os.popen方法三、commands模块四、subprocess模块五、总结:一、os.system方法
目录
  • 一、os.system方法
  • 二、os.popen方法
  • 三、commands模块
  • 四、subprocess模块
  • 五、总结:

一、os.system方法

这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息。

os.system(cmd)的返回值。如果执行成功,那么会返回0,表示命令执行成功。否则,则是执行错误。

使用os.system返回值是脚本的退出状态码,该方法在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码。

os.system()返回值为0 linux命令返回值也为0。

os.system()返回值为256,十六位二进制数示为:00000001,00000000,高八位转成十进制为 1 对应 linux命令返回值 1。

os.system()返回值为512,十六位二进制数示为:00000010,00000000,高八位转成十进制为 2 对应 linux命令返回值 2。

import os
result = os.system('cat /etc/passwd')
print(result)      # 0

二、os.popen方法

os.popen()方法不仅执行命令而且返回执行后的信息对象(常用于需要获取执行命令后的返回信息),是通过一个管道文编程客栈件将结果返回。通过 os.popen() 返回的是 file read 的对象,对其进行读取 rejsad() 的操作可以看到执行的输出。

import os
result = os.popen('cat /etc/passwd')
print(result.read())

三、commands模块

import commands
status = commands.getstatus('cat /etc/passwd')
print(status)
output = commands.getoutput('cat /etc/passwd')
print(output)
(status, output) = commands.getstatusoutput('cat /etc/passwd')
print(status, output)

四、subprocess模块

Subprocess是一个功能强大的子进程管理模块,是替换os.system,os.编程客栈spawn* 等方法的一个模块。

当执行命令的参数或者返回中包含了中文文字,那么建议使用subprocess。

import subprocess
res = su开发者_Pythonbprocess.Popen('cat /etc/passwd', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # 使用管道
print res.s编程客栈tdout.read()  # 标准输出
for line in res.stdout.readlines():
    print line
res.stdout.close()         # 关闭

五、总结:

os.system:获取程序执行命令的返回值。

os.popen: 获取程序执行命令的输出结js果。

commands:获取返回值和命令的输出结果。

到此这篇关于python调用系统命令的四种方法(os.system、os.popen、commands、subprocess)的文章就介绍到这了,更多相关Python调用系统命令内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号