Python 调用 Shell脚本的方法

1.os模块的popen方法 
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出。

>>> os.popen('date -u |wc')
<open file 'date -u |wc', mode 'r' at 0x7f9539eb34b0>
>>> os.popen('date -u |wc').read()
' 1 6 43\n'

2.利用commands模块 
这个模块有个非常好用的方法可以直接读取程序执行的返回值 
通过 commands.getstatusoutput() 一个方法就可以获得到返回值和输出

>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'

3.利用subprocess模块 
subprocess模块用来启动可终止其它程序,创建多个进程.想要shell中运行其它程序并获取它的输出,可以使用check_output()方法,它接受一个命令和参数列表

>>> subprocess.check_output(["echo", "Hello World!"])
'Hello World!\n'
>>> ret = subprocess.check_output(['date','-u'])
>>> ret
'2016\xe5\xb9\xb4 05\xe6\x9c\x88 20\xe6\x97\xa5 \xe6\x98\x9f\xe6\x9c\x9f\xe4\xba\x94 09:48:44 UTC\n'

本文转载自:https://blog.csdn.net/u010786109/article/details/51463598

Python 调用 Shell脚本的方法的更多相关文章

  1. 3种python调用其他脚本的方法,你还知道其他的方法吗?

    1.用python调用python脚本 #!/usr/local/bin/python3.7 import time import os count = 0 str = ('python b.py') ...

  2. python调用shell脚本时需要切换目录

    最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...

  3. python调用shell脚本

    # coding=utf-8   //设置文本格式import os            //导入os方法print('hello')n=os.system('/home/csliyb/kjqy_x ...

  4. python 调用shell命令的方法

    在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...

  5. python 调用shell命令三种方法

    #!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...

  6. python 调用 shell 命令

    记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");

  7. 【原】Gradle调用shell脚本和python脚本并传参

    最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...

  8. python 调用 shell 命令方法

    python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等   ...

  9. [Python]在python中调用shell脚本,并传入参数-02python操作shell实例

    首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell ...

随机推荐

  1. js和jquery获取屏幕的高度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  2. UDP也需要现有Server端,然后再有Client端

    UDP编程: DatagramSocket(邮递员):对应数据报的Socket概念,不需要创建两个socket,不可使用输入输出流. DatagramPacket(信件):数据包,是UDP下进行传输数 ...

  3. oracle oci 调用 1

    http://blog.163.com/earth_of_fire/blog/static/1368943200791211622278/(总结) http://blog.163.com/earth_ ...

  4. web 汇率

    http://www.cnblogs.com/beimeng/p/3789940.html 网站虽小,五脏俱全(干货)   前言 最近一个朋友让帮忙做一个汇率换算的网站,用业余时间,到最后总算是实现了 ...

  5. php -- 判断文件是否存在

    file_exists is_file is_dir 基本上,PHP的 file_exists = is_dir + is_file 写程序验证一下: 分别执行1000次,记录所需时间. ------ ...

  6. Java凝视模板

     设置凝视模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是全部需设置凝 ...

  7. 学习:base64和图片。

    一.网页即时聊天中,客户端A可以将图片转换为base64,发送到客户端B(可能经server端转发) 再由base64转换为图片. 关键字搜索:url.base64.

  8. Wamp2.5 64bit,无法改动MySQL datadir位置

    今天偶然想到去更新一下机子里面PHP的版本号,然后又一次去wamp官网下载了WAMP(wamp 64  Apache : 2.4.9 MySQL : 5.6.17 PHP : 5.5.12 PHPMy ...

  9. Python背景知识——学习笔记

    诞生于1989圣诞节,阿姆斯特丹.Guido van Rossum(吉多·范罗苏姆). Python Python:解释型.面向对象.动态数据类型 的 高级程序设计语言. 解释型语言:运行的时候将程序 ...

  10. Hadoop1.2.1 启停的Shell 脚本分析

    停止shell脚本以此类推.