Python—执行系统命令的四种方法

一、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。

1

2

3

import os

result = os.system('cat /etc/passwd')

print(result)      # 0

二、os.popen方法

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

1

2

3

import os

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

print(result.read())

三、commands模块

1

2

3

4

5

6

7

8

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。

1

2

3

4

5

6

import subprocess

res = subprocess.Popen('cat /etc/passwd', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # 使用管道

# print res.stdout.read()  # 标准输出

for line in res.stdout.readlines():

    print line

res.stdout.close()         # 关闭

五、总结:

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

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

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

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值