commands模块【转】
https://www.cnblogs.com/wjoyxt/p/5087951.html
要获得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
commands模块【转】的更多相关文章
- python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...
- python中的commands模块
commands模块用于调用shell命令 有3中方法: commands.getstatus() 返回执行状态 commands.getoutput() 返回执行结果 commands.ge ...
- python标准库介绍——34 commands 模块详解
==commands 模块== (只用于 Unix) ``commands`` 模块包含一些用于执行外部命令的函数. [Example 3-7 #eg-3-7] 展示了这个模块. ====Exampl ...
- Python的logging模块、os模块、commands模块与sys模块
一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...
- [Python2.x] 利用commands模块执行Linux shell命令
用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...
- python commands 模块
commands 模块 通过python调用系统命令 只适用于linux commands是提供linux系统环境下支持使用shell命令的一个模块 commands.getoutput(cmd) 只 ...
- python 基础 7.5 commands 模块
一. commands 模块 1.commands 模块只使用与linxu 的shell 模式下 在我们平时码字时,经常需要调用系统脚本或者系统命令来解决很多问题,接下来,我们就介绍给大家一个很好 ...
- 利用commands模块执行shell命令
利用commands模块执行shell命令 用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态 ...
- python commands模块在python3.x被subprocess取代
subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 ...
- python之commands模块
要获得shell命令的输出只需要`cmd`命令就可以了, 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果.看一下三个函数:1). comm ...
随机推荐
- 【HTML】解析原理
标准的web前端工程师需要知道 ◎浏览器(或者相应播放器)的渲染/重绘原理 这我得加把劲了.我还真的说的不是很清楚,我就G下,结果不是很多,找到了有一个,就记下来了... 以下部分来自handawei ...
- 如何重置Portal for ArcGIS、ArcGIS Server管理员密码
忘记管理员密码是ArcGIS系统管理员司空见惯的情况.每次为了找回站点管理员密码,用户经常要测试多次.有没有一种快捷的解决方案呢?答案是有的. 下面将分别介绍如何重置Portal for ArcGIS ...
- Ceph osd故障恢复
1 调高osd的日志等级 加上红框那一行就可以了 osd的日志路径:/var/log/ceph/ceph-osd.3.log 注意:加上了这一行后日志会刷很多,所以要特别注意日志容量的变化,以防把v ...
- 【BZOJ】BZOJ3040 最短路 线段树优化Dijkstra
题目描述 N个点,M条边的有向图,求点1到点N的最短路(保证存在). 1<=N<=1000000,1<=M<=10000000 输入格式 第一行两个整数N.M,表示点数和边数. ...
- Centos7变动
Nmtui:网络配置图形界面 Systemctl:管理systemd的单元 Service:系统服务 Socket:进程间的通信 Busname: Target:多个unit构成的组,运行级别 Sna ...
- JavaScript初探系列目录
一 系列导航 结合各方面的参考资料,整理出来以下主要目录,供方便浏览查看 (一)初探系列 JavaScript初探系列(1)——基本概念 JavaScript初探系列(2)——数 ...
- MYSQL | ERROR 1305(42000) SAVEPOINT *** DOES NOT EXIST
autocommit模式:在开启情况下,对于每条statement来说,都会自动形成一个commit,也就是会即时对开始和结束一个事务.所以,当出现rollback to savepoint出现这个错 ...
- Java IO系统--RandomAccessFile
RandomAccessFile 实现了DataOutput接口和DataInput接口.父类是Object,不继承任何的InputStream和OutStram. public class Rand ...
- linux 查看gpu信息
- openstack kvm cannot set up guest memory 'pc.ram': Cannot allocate memory
Kvm:启动报错:error: internal error: process exited while connecting to monitor: 2018-11-12T01:47:14.9933 ...