1》python调用Shell脚本,有两种方法:os.system()和os.popen(),
前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容。
>>>help(os.system)
Help on built-in function system in module posix:
system(...)
    system(command) -> exit_status
    Execute the command (a string) in a subshell.
>>> help(os.popen)
Help on built-in function popen in module posix:
popen(...)
    popen(command [, mode='r' [, bufsize]]) -> pipe
    Open a pipe to/from a command returning a file object.
2》假定有一个shell脚本test.sh:
song@ubuntu:~$ vi test.sh
song@ubuntu:~$ more test.sh
#!/bin/bash
echo 'hello python!'
echo 'hello world!'
exit 1
song@ubuntu:~$
2.1》os.system(command):该方法在调用完shell脚本后,返回一个16位的二进制数,
低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码,
即脚本中“exit 1”的代码执行后,os.system函数返回值的高位数则是1,如果低位数是0的情况下,
则函数的返回值是0x0100,换算为十进制得到256。
要获得os.system的正确返回值,可以使用位移运算(将返回值右移8位)还原返回值:
>>> import os
>>> os.system("./test.sh")
hello python!
hello world!
256
>>> n=os.system("./test.sh")
hello python!
hello world!
>>> n
256
>>> n>>8
1
>>>
2.2》os.popen(command):这种调用方式是通过管道的方式来实现,函数返回一个file对象,
里面的内容是脚本输出的内容(可简单理解为echo输出的内容),使用os.popen调用test.sh的情况:
>> import os
>>> os.popen("./test.sh")
<open file './test.sh', mode 'r' at 0x7f6cbbbee4b0>
>>> f=os.popen("./test.sh")
>>> f
<open file './test.sh', mode 'r' at 0x7f6cbbbee540>
>>> f.readlines()
['hello python!\n', 'hello world!\n']
>>>
3》像调用”ls”这样的shell命令,应该使用popen的方法来获得内容,对比如下:
>>> import os
>>> os.system("ls")   #直接看到运行结果
Desktop    Downloads     Music     Public     Templates  Videos
Documents  examples.desktop  Pictures  systemExit.py  test.sh
0    #返回值为0,表示命令执行成功
>>> n=os.system('ls')
Desktop    Downloads     Music     Public     Templates  Videos
Documents  examples.desktop  Pictures  systemExit.py  test.sh
>>> n
0
>>> n>>8   #将返回值右移8位,得到正确的返回值
0
>>> f=os.popen('ls') #返回一个file对象,可以对这个文件对象进行相关的操作
>>> f
<open file 'ls', mode 'r' at 0x7f5303d124b0>
>>> f.readlines()
['Desktop\n', 'Documents\n', 'Downloads\n', 'examples.desktop\n', 'Music\n', 'Pictures\n', 'Public\n', 'systemExit.py\n', 'Templates\n', 'test.sh\n', 'Videos\n']
>>>
总结:os.popen()可以实现一个“管道”,从这个命令获取的值可以继续被使用。因为它返回一个文件对象,可以对这个文件对象进行相关的操作。

但是如果要直接看到运行结果的话,那就应该使用os.system,用了以后,立竿见影!

python os.system()和os.popen()的更多相关文章

  1. python(45)内置函数:os.system() 和 os.popen()

    os.system() 和 os.popen() 概述 os.popen() 方法用于从一个命令打开一个管道. 在Unix,Windows中有效 语法 popen()方法语法格式如下: os.pope ...

  2. 调用系统命令 os.system()和os.popen()

    Python中os.system和os.popen区别 Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而 ...

  3. Python os.system 和 os.popen的区别

    (1) os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 system(command) -> exit_statusExecute the command ...

  4. Python中os.system和os.popen区别

    Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而选择. 两者的区别是: os.system(cmd)的返 ...

  5. python笔记16-执行cmd指令(os.system和os.popen)

    os.system 1.如果想在cmd执行python脚本,可以直接用如下指令 python [xx.py绝对路径] 比如我写了个hello.py的脚本,在脚本里面写入内容:print("h ...

  6. os.system和os.popen

    使用os.popen调用test.sh的情况: python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执 ...

  7. os.system() 和 os.popen()

    1.os.popen(command[, mode[, bufsize]])  os.system(command) 2.os.popen() 功能强于os.system() , os.popen() ...

  8. Python执行系统命令的方法 os.system(),os.popen(),commands

    os.popen():用python执行shell的命令,并且返回了结果,括号中是写shell命令 Python执行系统命令的方法: https://my.oschina.net/renwofei42 ...

  9. python os.system重定向stdout到变量 ,同时获取返回值

    Python执行系统命令的方法 os.system(),os.popen(),commands 最近在做那个测试框架的时候发现 Python 的另一个获得系统执行命令的返回值和输出的类. 最开始的时候 ...

随机推荐

  1. HDU 2612 find a way 【双BFS】

    <题目链接> 题目大意:两个人分别从地图中的Y 和 M出发,要共同在 @ 处会面(@不止有一处),问这两个人所走距离和的最小值是多少. 解题分析: 就是对这两个点分别进行一次BFS,求出它 ...

  2. PHP如何支持CURL字符串证书传输

    背景 最近在对接微信支付的时候,需要在退款处用到证书,由于我们是SAAS平台,要支持多方多渠道支付,如果把所有证书文件保存在应用服务器会受到SLB的影响,会导致某台机器文件不同步而阻碍退款流程,但把文 ...

  3. vue 开发环境搭建,超级简单仅需3步。

    1,打开 http://nodejs.cn/download/  下载 nodejs,并安装. 2,成功以后,启动cmd命令行,输入npm install -g cnpm --registry=htt ...

  4. bzoj 3811: 玛里苟斯

    3811: 玛里苟斯 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 190  Solved: 95[Submit][Status][Discuss] ...

  5. 潭州课堂25班:Ph201805201 并发 第九课 (课堂笔记)

    TCP/IP 1,建立连接(三次握手) 1,客户端发起请求 2,服务器请求回应 3,请求确认,双方建立连接 2,数据传输 3,断开连接(四次挥手) 1,客户端请求断开 连接 2,服务器回应请求 3,服 ...

  6. [USACO08DEC]Secret Message

    OJ题号: 洛谷2922 思路: 字典树,每个结点记录经过该节点的字符串数cnt和以该结点结尾的字符串数量val. 每次询问时累加经过节点的val值和结尾结点的cnt值. #include<cs ...

  7. angularJs中的checkboxs

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  8. Convert a VMDK (VMWare) file to VHDX (Hyper-V)

    https://www.meziantou.net/2016/09/09/convert-a-vmdk-vmware-file-to-vhdx-hyper-v Microsoft provides a ...

  9. SVN解决本地版本控制与服务器版本冲突问题

    最近经常遇到一个冲突问题,svn服务器已经没有这个文件,本地也没有这个文件,但是你提交代码的时候svn会显示冲突,也就是说本地svn仍然在管理着这个文件. 解决办法:在相应的目录下新建一个名字一样的文 ...

  10. openvpn 的安装和使用方法

    一.安装: 必备条件: 该计算机必须安装了网卡 1.像安装普通软件一样将 openvpn 这个软件安装好 2.到安装目录下/config 中,该目录下默认只有一个 README.txt,然后将企业给的 ...