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. linphone 调试信息

    root@phyCORE-AM335x:~ linphonec -V -d 6INFO: no logfile, logging to stdoutortp-message-oRTP-0.20.0 i ...

  2. linux - fpga-framebuff驱动

    * linux/drivers/video/fpga_fb.c --fpga graphics adaptor frame buffer device *  Created 16 Sep2011 *  ...

  3. Extjs GridPanel 中放入 Combox显示问题

    http://weijun8611-126-com.iteye.com/blog/566201 在项目中使用了extjs的editorgridpanel,但是其中的combobox在选择了相应的选项后 ...

  4. 我在GitHub Pages托管静态博客啦~

    https://jinhengyu.github.io/

  5. SQL简明教程系列15 创建索引

    CREATE INDEX用于在表中创建索引. 索引使数据库应用程序可以更快地查找数据. 注:更新一个包含索引的表比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新.因此,理想的做法是仅仅在常 ...

  6. php if语句判定my查询是否为空

    <?php header("Content-type: text/html; charset=utf-8"); $username=$_GET['username']; $p ...

  7. 第二百六十三节,Tornado框架-基于正则的动态路由映射

    Tornado框架-基于正则的动态路由映射 1.在路由映射条件里用正则匹配访问路径后缀2.给每一个正则匹配规则(?P<设置名称>)设置一个名称,3.在逻辑处理的get()方法或post() ...

  8. hrbustoj 1306:再遇攻击(计算几何,判断点是否在多边形内,水题)

    再遇攻击 Time Limit: 1000 MS    Memory Limit: 65536 K Total Submit: 253(37 users)   Total Accepted: 56(2 ...

  9. Myeclipse 10 Maven 构建 Java Web 项目

    主要介绍如何使用 Myeclipse 10 构建 Maven Web 项目,关于 Maven 的介绍就略过了. 工具/原料 myeclipse apache-maven-3.1.0 方法/步骤 1 下 ...

  10. 杂记之--苹果4s手机呼叫转移怎么设置

    您好,呼叫转移只需在拨号界面输入相应的代码就可以,无需其他设置无条件转移 **21*电话号码#再按拨号键 取消代码:##21# *#21# 再按拨号键无信号,关机转移 **62*电话号码#再按拨号键 ...