python中os.system、os.popen、subprocess.popen的区别


最近项目中需要在python中执行shell脚本,以下解释使用os.system、
os.popen和subprocess.popen的区别:

1. os.system

该函数返回命令执行结果的返回值,system()函数在执行过程中进行了以下三步操作:

  1. fork一个子进程;
  2. 在子进程中调用exec函数去执行命令;
  3. 在父进程中调用wait(阻塞)去等待子进程结束。

对于fork失败,system()函数返回-1。
由于使用该函数经常会莫名其妙地出现错误,但是直接执行命令并没有问题,所以一般建议不要使用。

2. os.popen

popen() 创建一个管道,通过fork一个子进程,然后该子进程执行命令。返回值在标准IO流中,该管道用于父子进程间通信。父进程要么从管道读信息,要么向管道写信息,至于是读还是写取决于父进程调用popen时传递的参数(w或r)。通过popen函数读取命令执行过程中的输出示例如下:

#!/usr/bin/python
import os

p=os.popen('ssh 10.3.16.121 ls')
x=p.read()
print x
p.close()

3. subprocess模块

1)概述

subprocess模块是在2.4版本中新增的,官方文档中描述为可以用来替换以下函数:
os.system、os.spawn、os.popen、popen2

2)参数

官方对于subprocess模块的参数解释如下:

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

subprocess

参数既可以是string,也可以是list。
subprocess.Popen([“cat”,”test.txt”])
subprocess.Popen(“cat test.txt”, shell=True)
对于参数是字符串,需要指定shell=True

3)使用示例
其中subprocess.call用于代替os.system,示例:

import subprocess
returnCode = subprocess.call('adb devices')
print returnCode
  • subprocess.check_output

  • subprocess.Popen的使用

  1. 执行结果保存在文件
cmd = "adb shell ls /sdcard/ | findstr aa.png"  
fhandle = open(r"e:\aa.txt", "w")  
pipe = subprocess.Popen(cmd, shell=True, stdout=fhandle).stdout  
fhandle.close()  
  1. 执行结果使用管道输出
pipe=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE).stdout  
print pipe.read() 

4. commands.getstatusoutput()

使用commands.getstatusoutput() 方法就可以获得到返回值和输出:

(status, output) = commands.getstatusoutput('sh hello.sh')
print status, output
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`os.system`、`os.popen`和`subprocess.Popen`都是用于在Python程序调用系统命令的函数,但是它们在用法和功能上有所不同。 1. `os.system` `os.system`函数用于在操作系统执行命令,并返回执行结果。它的使用方法是: ```python import os os.system('command') ``` 其,`command`是要执行的命令,可以是任何在操作系统可执行的命令。`os.system`函数将返回命令的退出状态码,通常情况下,0表示执行成功,其他值表示执行失败。 2. `os.popen` `os.popen`函数用于在操作系统执行命令,并返回命令的输出。它的使用方法是: ```python import os output = os.popen('command').read() ``` 其,`command`是要执行的命令,`output`是命令的输出结果,它是一个字符串类型的变量。`os.popen`函数将执行命令,并将命令的输出保存到`output`。 3. `subprocess.Popen` `subprocess.Popen`函数也用于在操作系统执行命令,但是它提供了更丰富的控制和选项。它的使用方法是: ```python import subprocess p = subprocess.Popen('command', stdout=subprocess.PIPE, shell=True) output, errors = p.communicate() ``` 其,`command`是要执行的命令,`stdout=subprocess.PIPE`表示将命令的输出保存到`output`,`shell=True`表示可以执行shell命令。`subprocess.Popen`函数将执行命令,并返回一个Popen对象,通过Popen对象可以控制命令的执行和获取命令的输出。 以上是三种调用系统命令的方法的区别和用法。总体来说,`os.system`函数用于简单的命令调用,`os.popen`函数用于获取命令的输出,`subprocess.Popen`函数用于更复杂的命令调用和控制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值