首先使用内置模块os.

>>> import os

>>> code = os.system("pwd && sleep 2")

# /User/zhipeng

>>> print code

# 0

问题是 os.system 只能获取到结束状态

使用内置模块 subprocess

>>> import subprocess

>>> subprocess.Popen("pwd && sleep 2", shell=True, cwd="/home")

#

# /home

>>> sub = subprocess.Popen("pwd && sleep 2", shell=True, stdout=subprcess.PIPE)

>>> sub.wait()

>>> print sub.stdout.read()

# /User/zhipeng

subprocess.Popen还支持一些别的参数

bufsize,executable=None, stdin=None, stdout=None, stderr=None

preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None

universal_newlines=False, startupinfo=None, creationflags=0

使用第三方模块 sh

# pip install sh

>>> from sh import ifconfig

>>> print ifconfig("eth0")

>>> from sh import bash

>>> bash("pwd")

Traceback (most recent call last):

File "", line 1, in

File "/Library/Python/2.7/site-packages/sh.py", line 1021, in __call__

return RunningCommand(cmd, call_args, stdin, stdout, stderr)

File "/Library/Python/2.7/site-packages/sh.py", line 486, in __init__

self.wait()

File "/Library/Python/2.7/site-packages/sh.py", line 500, in wait

self.handle_command_exit_code(exit_code)

File "/Library/Python/2.7/site-packages/sh.py", line 516, in handle_command_exit_code

raise exc(self.ran, self.process.stdout, self.process.stderr)

sh.ErrorReturnCode_126:

RAN: '/bin/bash ls'

STDOUT:

STDERR:

/bin/ls: /bin/ls: cannot execute binary file

# 不能这么用

>>> from sh import ls

>>> ls()

# hello.txt 1.txt

# ls -al

>>> ls(a=True, l=True)

# ls(al=True) 是不可以的

这操作太复杂了, 项目中使用也太糟心了, 也没有办法多个命令同时用.不过可以用别的方式代替

# bash -c command 可以很好的解决这个问题

# bash -c "sleep 1 && pwd"

>>> result = bash(c="pwd", _timeout=1, _cwd="/home")

>>> print result

# -rw-r--r--@ 1 zhipeng staff 0 10 13 18:30 hello.txt

# -rw-r--r--@ 1 zhipeng staff 0 10 13 18:30 1.txt

>>> result = bash(c="pwd", _timeout=1, _cwd="/")

>>> print result

# /

>>> bash(c="pwd && sleep 2", _timeout=1)

Traceback (most recent call last):

File "", line 1, in

File "/Library/Python/2.7/site-packages/sh.py", line 1021, in __call__

return RunningCommand(cmd, call_args, stdin, stdout, stderr)

File "/Library/Python/2.7/site-packages/sh.py", line 486, in __init__

self.wait()

File "/Library/Python/2.7/site-packages/sh.py", line 498, in wait

raise TimeoutException(-exit_code)

sh.TimeoutException

参数里面可以添加非命令参数. 需要以_开头, 例如上面的_timeout, _cwd. 详见sh.py 源码

还支持以下参数

internal_bufsize, err_bufsize, tee, done, in, decode_errors, tty_in,

out, cwd, timeout_signal, bg, timeout, with, ok_code, err, env, no_out,

参考:

https://github.com/amoffat/sh/blob/master/sh.py

https://github.com/amoffat/sh

以上这篇Python 运行 shell 获取输出结果的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

python运行结果图_[宜配屋]听图阁相关推荐

  1. 太极图python自定义函数绘制_[宜配屋]听图阁

    效果如下所示: # -*- coding: utf-8 -*- import turtle # 绘制太极图函数 def draw_TJT(R): turtle.screensize(800, 600, ...

  2. python写抽奖转盘_[宜配屋]听图阁

    本文实例为大家分享了python实现转盘效果的具体代码,供大家参考,具体内容如下 #抽奖 面向对象版本 import tkinter import time import threading clas ...

  3. python上下文管理关键字_[宜配屋]听图阁

    前言 如果你有阅读源码的习惯,可能会看到一些优秀的代码经常出现带有 "with" 关键字的语句,它通常用在什么场景呢?今天就来说说 with 和 上下文管理器. 对于系统资源如文件 ...

  4. python tkinter布局混用_[宜配屋]听图阁

    这篇文章主要介绍了python tkinter控件布局项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码部分: from tkinter ...

  5. python调用百度地图画轨迹图_[宜配屋]听图阁

    如题,先上效果图: 主要分为两大步骤 使用python语句,通过百度地图API,对已知的地名抓取经纬度 使用百度地图API官网的html例程,修改数据部分,实现呈现效果 一.使用python语句,通过 ...

  6. python图片横向合并_[宜配屋]听图阁

    起因: 有一批数据需要每个月进行分析,数据存储在excel中,行标题一致,需要横向合并进行分析. 数据示意: 具有多个 代码: # -*- coding: utf-8 -*- "" ...

  7. python画平行坐标图_[宜配屋]听图阁

    平行坐标图,一种数据可视化的方式.以多个垂直平行的坐标轴表示多个维度,以维度上的刻度表示在该属性上对应值,相连而得的一个折线表示一个样本,以不同颜色区分类别. 但是很可惜,才疏学浅,没办法在Pytho ...

  8. python整型图_[宜配屋]听图阁

    Python3支持三种不同的数值类型: 整型(int)--通常被称为是整型或整数,可以是正整数或负整数,不带小数点.Python3整型是没有限制大小的,可以当做long类型使用, 但实际上由于机器内存 ...

  9. python怎么放音乐_[宜配屋]听图阁

    本文实例使用Tkinter实现在线音乐播放器的具体代码,供大家参考,具体内容如下 1.先使用Tkinter库写界面 2.写点击按钮触发的事件 (1).使用网易音乐的api,返回数据包装成json格式数 ...

最新文章

  1. Java 技术篇-IntelliJ IDEA修改java、jdk版本实例演示
  2. 词法分析是否需要处理负数
  3. 【Java深入研究】2、JDK 1.8 LinkedList源码解析
  4. WACV 2021 论文大盘点 目标检测与图像分割篇(持续更新)
  5. C++ 中复杂的声明
  6. Install Visual Studio
  7. Android 获取静态上下文(Application)
  8. [转]HSPICE软件的应用及常见问题解决
  9. 从苹果店员到机器学习工程师:学习AI,我是这样起步的
  10. 1067 Sort with Swap(0, i) (25 分) 好,容易出错
  11. linux生成可执行文件的过程
  12. bitbake如何clean
  13. 文件共享 无法访问,你可能没有权限使用网络资源,请与这台服务器的管理员联...
  14. Ubuntu卸载历程,包含重启进入grub解决方案
  15. 六年的计算机电源坏啦,电脑电源坏了有什么症状
  16. 开源私有lorawan server搭建
  17. Python利用Opencv读取图片
  18. golang []byte转json和json转[]byte
  19. Carson带你学Android:RxJava过滤操作符
  20. python bokeh_使用 Bokeh 为你的 Python 绘图添加交互性

热门文章

  1. 实现两个数的交换(异或,加减)
  2. excel连接mysql 数据库
  3. JAVA中断线程的方法
  4. linux 系统安装mongodb数据库---方法1
  5. uni-app使用前的调研和开发心得
  6. AUTOSAR协议解析篇(一)-J1939协议解析
  7. 一文教你掌握 ZooKeeper 核心知识
  8. 济宁医学院计算机专业好就业吗,山东这3所医学院实力强,就业率高,中等生可捡漏...
  9. zookeeper删除节点的权限_Zookeeper使用超级用户删除带权限的节点
  10. gitlab将分支代码合并到master上