python之commands模块
要获得shell命令的输出只需要`cmd`命令就可以了,
需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果.
看一下三个函数:
1). commands.getstatusoutput(cmd)
用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result),其中 status为int类型,result为string类型。cmd执行的方式是{ cmd ; } 2>&1, 这样返回结果里面就会包含标准输出和标准错误.
2). commands.getoutput(cmd)
只返回执行的结果, 忽略返回值.
3). commands.getstatus(file) #现已被弃用
返回ls -ld file执行的结果.
看一下这些函数使用的例子:
>>> 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') #该函数已被python丢弃,不建议使用,它返回 ls -ld file 的结果(String)(返回结果太奇怪了,难怪被丢弃)
'-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls'
#!/usr/bin/python
#coding:utf-8
import os,sys,commands def openfile():
grains = {}
_open_file=65533
try:
getulimit=commands.getstatusoutput('source /etc/profile;ulimit -n')
except Exception,e:
pass
if getulimit[0]==0:
_open_file=int(getulimit[1])
grains['max_open_file'] = _open_file
return grains
python之commands模块的更多相关文章
- [Python] 利用commands模块执行Linux shell命令
http://blog.csdn.net/dbanote/article/details/9414133 http://zhou123.blog.51cto.com/4355617/1312791
- python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...
- Python的logging模块、os模块、commands模块与sys模块
一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...
- python之返回状态commands模块
需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands很容易做到以上的效果. commands.getstatusoutput(cmd) 返回一个元组(status, ...
- python commands 模块
commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...
- python 基础 7.5 commands 模块
一. commands 模块 1.commands 模块只使用与linxu 的shell 模式下 在我们平时码字时,经常需要调用系统脚本或者系统命令来解决很多问题,接下来,我们就介绍给大家一个很好 ...
- 学习python os commands socket模块
import os print(os.getcwd()) #获取当前路径, 导包也是从这个路径下面才能找到 # os.chdir('./..') #返回上一级路径,再获取路径看看 # print(os ...
- python之commands和subprocess入门介绍(可执行shell命令的模块)
一.commands模块 1.介绍 当我们使用Python进行编码的时候,但是又想运行一些shell命令,去创建文件夹.移动文件等等操作时,我们可以使用一些Python库去执行shell命令. com ...
- python中的commands模块
commands模块用于调用shell命令 有3中方法: commands.getstatus() 返回执行状态 commands.getoutput() 返回执行结果 commands.ge ...
随机推荐
- FastAdmin 开发第三天:认识目录
以下为标准 FastAdmin 安装后的目录 我们在运行命令时都是在这个目录. 我们所有的命令都在这个目录下面运行. 比如:安装前端组件,bower install 安装php 组件 composer ...
- tomcat源码阅读之部署器
我们知道web应用是用Context实例表示的,而Context是部署到Host实例中的,因此tomcat的部署器是关联的Host实例.Context实例可以用WAR文件部署,也可以把整个web应用的 ...
- IE版本检测
<html><body><script type="text/javascript">var browser=navigator.appName ...
- bzoj 4137 [FJOI2015]火星商店问题——线段树分治+可持久化01trie树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4137 关于可持久化01trie树:https://www.cnblogs.com/LadyL ...
- Microsoft Dynamics CRM service 创建,更新等操作时,注意写抛出异常时,抛出SoapException异常
具体如下: using System.Web.Services.Protocols; try{ crmService.Update(procurementPlanEntity);//更新操作}catc ...
- 大快DKH大数据智能分析平台监控参数说明
2018年国内大数据公司50强榜单排名已经公布了出来,大快以黑马之姿闯入50强,并摘得多项桂冠.Hanlp自然语言处理技术也荣膺了“2018中国数据星技术”奖.对这份榜单感兴趣的可以找一下看看.本篇承 ...
- Spring+hibernate+mysql事物不回滚的原因以及处理
最近项目突然出了点问题,然后发现用Service层下面的一个类的一个方法里的事务居然没有回滚.然后自己写了一个测试方法经过了N次测试都是不回滚.以下是测试方法的一部分: @Transactional( ...
- JsonCpp使用方法详解
JSON全称为JavaScript ObjectNotation,它是一种轻量级的数据交换格式,易于阅读.编写.解析.jsoncpp是c++解析JSON串常用的解析库之一. jsoncpp中主要的类: ...
- springMVC学习(4)-商品修改(RequestMapping解释、controller返回值)
一.需求: 操作流程: 1.进入商品查询列表页面 2.点击修改,进入商品修改页面,页面中显示了要修改的商品(从数据库查询) 3.在商品修改页面,修改商品信息,修改后,点击提交 代码: ItemsMap ...
- java中使HttpDelete可以发送body信息
java中使HttpDelete可以发送body信息RESTful api中用到了DELETE方法,android开发的同事遇到了问题,使用HttpDelete执行DELETE操作的时候,不能携带bo ...