python中得到shell命令输出的方法
import subprocess def execute_cmd(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output, err = p.communicate()
# 判断命令是否执行成功
status = 1 if err else 0
if status == 0:
print('[SUCCESS] %s' % cmd)
else:
print('[ERROR] %s' % cmd)
print(err)
return status, output
import os process = os.popen('ls -l') # return file
output = process.read()
process.close() # 简单写
with os.popen('ls -l') as process:
output = process.read()
python 2.7
import commands return_code, output = commands.getstatusoutput('ls -l')
python3
import subprocess def execute_cmd(cmd)
''' status: 0: success, 1: error
ret: success: stdout, error: stderr
'''
status, ret = subprocess.getstatusoutput(cmd)
if status == 0:
print('[SUCCESS] %s' % cmd)
else:
print('[ERROR] %s' % cmd)
print(ret)
return status, ret
python中得到shell命令输出的方法的更多相关文章
- python中执行shell命令的几个方法小结(转载)
转载:http://www.jb51.net/article/55327.htm python中执行shell命令的几个方法小结 投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014- ...
- python中执行shell的两种方法总结
这篇文章主要介绍了python中执行shell的两种方法,有两种方法可以在Python中执行SHELL程序,方法一是使用Python的commands包,方法二则是使用subprocess包,这两个包 ...
- 「Python」6种python中执行shell命令方法
用Python调用Shell命令有如下几种方式: 第一种: os.system("The command you want"). 这个调用相当直接,且是同步进行的,程序需要阻塞并等 ...
- python中执行shell命令的几个方法小结
原文 http://www.jb51.net/article/55327.htm 最近有个需求就是页面上执行shell命令,第一想到的就是os.system, os.system('cat /proc ...
- 【python中调用shell命令使用PIPE】使用PIPE作为stdout出现假卡死的情况——将stdout重定向为输出到临时文件
在Python中,调用:subprocess.Popen(cmd, stdout = PIPE, stderr = PIPE, shell= true)的时候,如果调用的shell命令本身在执行之后会 ...
- python中执行shell命令行read结果
+++++++++++++++++++++++++++++ python执行shell命令1 os.system 可以返回运行shell命令状态,同时会在终端输出运行结果 例如 ipython中运行如 ...
- python 中调用shell命令
subprocess模块 根据Python官方文档说明,subprocess模块用于取代上面这些模块.有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用sub ...
- 解析如何在C语言中调用shell命令的实现方法【转】
本文转自:http://www.jb51.net/article/37404.htm 1.system(执行shell 命令)相关函数 fork,execve,waitpid,popen表头文件 #i ...
- python中执行shell命令的几个方法
1.os.system() a=os.system("df -hT | awk 'NR==3{print $(NF-1)}'") 该命令会在页面上打印输出结果,但变量不会保留结果, ...
随机推荐
- go context包的WithTimeout和WithCancel的使用
1.WaitGroup 它是一种控制并发的方式,它的这种方式是控制多个goroutine同时完成. func main() { var wg sync.WaitGroup wg.Add(2) go f ...
- UCI机器学习库和一些相关算法(转载)
UCI机器学习库和一些相关算法 各种机器学习任务的顶级结果(论文)汇总 https://github.com//RedditSota/state-of-the-art-result-for-machi ...
- Django1.6 +wsgi 部署到Apache2 的步骤。
网上很多教程都是关于1.6之前的版本,很多都不适用,经历告诉我们最靠谱的还是官方文档. 一个Demo例子: 以 python shell开发的方式部署没有问题,但当独立部署到Apache2的过程非常艰 ...
- linux TZ格式
man tzset可以很清楚了解时区设置格式,共3种: The first format is used when there is no daylight saving time in the lo ...
- IPC之SystemV
svipc - System V interprocess communication mechanisms linux实现的System V interprocess communication ( ...
- RTT工程管理
一.RTT工程管理 RTT采用SCons管理工程. 本次安装版本:Python-2.7.3.1.exe,python-2.7.11.msi,scons-2.3.1-setup.exe 安装完成后,需要 ...
- PAT006 Tree Traversals Again
题目: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exa ...
- Linux下面 多线程死锁问题的调试
最近写服务,经常是单进程,多线程的,加了各种锁,很担心出现死锁问题,专门学习了一下死锁问题的诊断. 死锁 (deallocks): 是指两个或两个以上的进程(线程)在执行过程中,因争夺资源而造成的一种 ...
- 【c语言】将正数变成相应的负数,将负数变成相应的正数
<pre name="code" class="cpp">// 将正数变成相应的负数,将负数变成相应的正数 #include <stdio.h ...
- trait优先级 与 使用
之前一直沒有讲到trait,在此我不得不提一下trait中的优先级: 在trait继承中,优先顺序依次是:来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法. For e ...