1 简单调用shell命令

os.system(command)

在一个子shell中运行command命令, 并返回command命令执行完毕后的退出状态. 这实际上是使用C标准库函数system()实现的. 这个函数在执行command命令时需要重新打开一个终端, 并且无法保存command命令的执行结果.

2 通过管道

os.popen(command, mode)

打开一个与command进程之间的管道. 这个函数的返回值是一个文件对象, 可以读或者写(由mode决定, mode默认是’r'). 如果mode为’r', 可以使用此函数的返回值调用read()来获取command命令的执行结果.

例如:

写管道:

os.popen('wc -c', 'w').write('hello world') 

读管道:

print os.popen("echo 'hello world' |wc -c").read()

3 commands

import commands
commands.getstatusoutput(command)

使用os.popen()函数执行command命令并返回一个元组(status, output), 分别表示:

  • command命令执行的返回状态(相当于命令行查看$?)
  • 执行结果(相当于命令行输出)

对command的执行实际上是按照{command;} 2>&1的方式, 所以output中包含控制台输出信息或者错误信息. output中不包含尾部的换行符.

4 subprocess

  • subprocess.call(['some_command','some_argument','another_argument_or_path'])
  • subprocess.call(command,shell=True)
  • subprocess.Popen(command, shell=True)

如果command不是一个可执行文件, shell=True不可省.

使用subprocess模块可以创建新的进程, 可以与新建进程的输入/输出/错误管道连通, 并可以获得新建进程执行的返回状态. 使用subprocess模块的目的是替代os.system(), os.popen(), commands.*等旧的函数或模块.

最简单的方法是使用class subprocess.Popen(command, shell=True). Popen类有Popen.stdin, Popen.stdout, Popen.stderr三个有用的属性, 可以实现与子进程的通信.

将调用shell的结果赋值给python变量. 如:

handle = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
print handle.communicate()[0]

>>> import subprocess
>>> path = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE)
>>> print(path.communicate()[0])
/proc/4469

 

5 参考

http://blog.sina.cn/dpool/blog/s/blog_5357c0af0100yzet.html?vt=4

http://m.jb51.net/article/43500.htm

http://www.mikewootc.com/wiki/sw_develop/language/python_shell.html

Python中调用shell的更多相关文章

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

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

  2. python 中调用shell命令

    subprocess模块 根据Python官方文档说明,subprocess模块用于取代上面这些模块.有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用sub ...

  3. 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件

    在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...

  4. python中执行shell的两种方法总结

    这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...

  5. python中写shell(转)

    python中写shell,亲测可用,转自stackoverflow To run a bash script, copy from stackoverflow def run_script(scri ...

  6. Awk中调用shell命令

    Awk中调用shell命令 需求 在awk中,有时候需要调用linux系统中命令,如计算字符串的MD5值,并保存下来. 方法参考 call a shell command from inside aw ...

  7. 【转载】如何在C语言中调用shell命令

    转载自:http://blog.csdn.net/chdhust/article/details/7951576 如何在C语言中调用shell命令 在linux操作系统中,很多shell命令使用起来非 ...

  8. [转] Makefile中调用Shell

    1.在Makefile中只能在target中调用Shell脚本,其他地方是不能输出的.比如如下代码就是没有任何输出: VAR="Hello" echo "$(VAR)&q ...

  9. Python脚本传參和Python中调用mysqldump

    Python脚本传參和Python中调用mysqldump<pre name="code" class="python">#coding=utf-8 ...

随机推荐

  1. [转载]AdaBoost算法

    [转载]AdaBoost算法 原文:https://blog.csdn.net/v_july_v/article/details/40718799 这里就不转载了,到原文看吧.但是有几点可以注意下: ...

  2. NSInvocation简单总结

    (1)用法 NSInvocation是调用函数的另一种方式,它将调用者,函数名,参数封装到一个对象,然后通过一个invoke函数来执行被调用的函数,其思想就是命令者模式,将请求封装成对象. 例如,有这 ...

  3. Django 开发相关知识 整理

    前言 前端ajax HTTP请求头 ajax上传文件 jsonp跨域 URL 设计 分发 url参数编码 反向生成url 视图 request对象 POST url信息 视图返回值 HttpRespo ...

  4. redis系列一: windows下安装redis

    一. 下载Redis Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情况选择,这里我们下载 Redis-x64-xxx.zip压缩包到 C 盘,解压后,将文件夹重新命名为 red ...

  5. Ubuntu在命令行安装显卡驱动

    Ubuntu在命令行安装显卡驱动 1.进入电脑的BIOS,把Security Boot设置为Disabled. 2.进入终端,输入一以下命令(这里以安装NVIDIA-390进行演示) sudo add ...

  6. 【wlan】iw命令集合

    查看所连接的客户端的个数和信息 iw dev wlan0 station dump root@OpenWrt:~# iw dev wlan0 station dump Station e0:ac:cb ...

  7. SSH环境搭建之Spring环境搭建篇

    SSH环境搭建之Spring环境搭建篇 一.引入Spring所使用的JAR文件 二.在src目录下创建beans.xml(Spring的容器文件) <?xml version="1.0 ...

  8. ssh推送安装mysql脚本

    [root@tianyun project]# vim mysql_install_1.sh #! /usr/bin/env bash # mysql install 1 # by tianyun w ...

  9. 音频转换 wav to wav、mp3或者其它

    1.首先介绍一种NAudio 的方式 需要导入 NAudio.dll 下面请看核心代码 using (WaveFileReader reader = new WaveFileReader(in_pat ...

  10. Java多线程断点下载文件

    Java实现断点续传+多线程下载 如下代码所示,每一步都有注解 思路: 通过URL连接到服务器上要下载的文件,得到文件的大小: 算出每条线程下载的开始位置和结束位置,例如,有两条线程下载100Byte ...