python运行结果图_[宜配屋]听图阁
首先使用内置模块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运行结果图_[宜配屋]听图阁相关推荐
- 太极图python自定义函数绘制_[宜配屋]听图阁
效果如下所示: # -*- coding: utf-8 -*- import turtle # 绘制太极图函数 def draw_TJT(R): turtle.screensize(800, 600, ...
- python写抽奖转盘_[宜配屋]听图阁
本文实例为大家分享了python实现转盘效果的具体代码,供大家参考,具体内容如下 #抽奖 面向对象版本 import tkinter import time import threading clas ...
- python上下文管理关键字_[宜配屋]听图阁
前言 如果你有阅读源码的习惯,可能会看到一些优秀的代码经常出现带有 "with" 关键字的语句,它通常用在什么场景呢?今天就来说说 with 和 上下文管理器. 对于系统资源如文件 ...
- python tkinter布局混用_[宜配屋]听图阁
这篇文章主要介绍了python tkinter控件布局项目实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码部分: from tkinter ...
- python调用百度地图画轨迹图_[宜配屋]听图阁
如题,先上效果图: 主要分为两大步骤 使用python语句,通过百度地图API,对已知的地名抓取经纬度 使用百度地图API官网的html例程,修改数据部分,实现呈现效果 一.使用python语句,通过 ...
- python图片横向合并_[宜配屋]听图阁
起因: 有一批数据需要每个月进行分析,数据存储在excel中,行标题一致,需要横向合并进行分析. 数据示意: 具有多个 代码: # -*- coding: utf-8 -*- "" ...
- python画平行坐标图_[宜配屋]听图阁
平行坐标图,一种数据可视化的方式.以多个垂直平行的坐标轴表示多个维度,以维度上的刻度表示在该属性上对应值,相连而得的一个折线表示一个样本,以不同颜色区分类别. 但是很可惜,才疏学浅,没办法在Pytho ...
- python整型图_[宜配屋]听图阁
Python3支持三种不同的数值类型: 整型(int)--通常被称为是整型或整数,可以是正整数或负整数,不带小数点.Python3整型是没有限制大小的,可以当做long类型使用, 但实际上由于机器内存 ...
- python怎么放音乐_[宜配屋]听图阁
本文实例使用Tkinter实现在线音乐播放器的具体代码,供大家参考,具体内容如下 1.先使用Tkinter库写界面 2.写点击按钮触发的事件 (1).使用网易音乐的api,返回数据包装成json格式数 ...
最新文章
- Java 技术篇-IntelliJ IDEA修改java、jdk版本实例演示
- 词法分析是否需要处理负数
- 【Java深入研究】2、JDK 1.8 LinkedList源码解析
- WACV 2021 论文大盘点 目标检测与图像分割篇(持续更新)
- C++ 中复杂的声明
- Install Visual Studio
- Android 获取静态上下文(Application)
- [转]HSPICE软件的应用及常见问题解决
- 从苹果店员到机器学习工程师:学习AI,我是这样起步的
- 1067 Sort with Swap(0, i) (25 分) 好,容易出错
- linux生成可执行文件的过程
- bitbake如何clean
- 文件共享 无法访问,你可能没有权限使用网络资源,请与这台服务器的管理员联...
- Ubuntu卸载历程,包含重启进入grub解决方案
- 六年的计算机电源坏啦,电脑电源坏了有什么症状
- 开源私有lorawan server搭建
- Python利用Opencv读取图片
- golang []byte转json和json转[]byte
- Carson带你学Android:RxJava过滤操作符
- python bokeh_使用 Bokeh 为你的 Python 绘图添加交互性