commands 模块

通过python调用系统命令 只适用于linux

commands是提供linux系统环境下支持使用shell命令的一个模块

commands.getoutput(cmd)

只返回执行shell命令的结果

import commands

cmd = 'ls /opt'

a = commands.getoutput(cmd)

print(type(a))
print(a) # <type 'str'>
# auto-mongo-primary.sh
# install-zabbix.sh
# python.py
# soft

commands.getstatusoutput(cmd)

import commands
cmd = 'ls /home/admin'
c = commands.getstatusoutput(cmd)
print(type(c))
status, output = commands.getstatusoutput(cmd)
print(status)
print(output)
print(type(output)) # <type 'str'>
# 0
# auto-mongo-primary.sh
# install-zabbix.sh
# python.py
# soft

返回结果是一个tuple,第一个值是shell执行的结果,如果shell执行成功,返回0,否则,为非0,第二个是一个字符串,就是我们shell命令的执行结果,python通过一一对应的方式复制给status和output

python commands 模块的更多相关文章

  1. python commands模块在python3.x被subprocess取代

    subprocess 可以执行shell命令的相关模块和函数有: os.systemos.spawnos.popen --废弃popen2.* --废弃commands.* --废弃,3.x中被移除 ...

  2. python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...

  3. Python的logging模块、os模块、commands模块与sys模块

    一.logging模块 import logging logging.debug('This is debug message') logging.info('This is info message ...

  4. python 基础 7.5 commands 模块

    一. commands 模块   1.commands 模块只使用与linxu 的shell 模式下 在我们平时码字时,经常需要调用系统脚本或者系统命令来解决很多问题,接下来,我们就介绍给大家一个很好 ...

  5. python中的commands模块

    commands模块用于调用shell命令 有3中方法: commands.getstatus()   返回执行状态 commands.getoutput()   返回执行结果 commands.ge ...

  6. python标准库介绍——34 commands 模块详解

    ==commands 模块== (只用于 Unix) ``commands`` 模块包含一些用于执行外部命令的函数. [Example 3-7 #eg-3-7] 展示了这个模块. ====Exampl ...

  7. Day05 - Python 常用模块

    1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...

  8. python 各模块

    01 关于本书 02 代码约定 03 关于例子 04 如何联系我们 1 核心模块 11 介绍 111 内建函数和异常 112 操作系统接口模块 113 类型支持模块 114 正则表达式 115 语言支 ...

  9. Day5 模块及Python常用模块

    模块概述 定义:模块,用一砣代码实现了某类功能的代码集合. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,提供了代码的重用性.在Python中,一个.py文件就称之为一个模块(Mod ...

随机推荐

  1. vue插件库

    各种vue插件,各种有,总有一款适合你! github地址:https://github.com/opendigg/awesome-github-vue

  2. django处理上传文件配置

    1.sttings中配置 'django.template.context_processors.media' TEMPLATES = [ { 'BACKEND': 'django.template. ...

  3. poj 3045 叠罗汉问题 贪心算法

    题意:将n头牛叠起来,每头牛的力气 s体重 w  倒下的风险是身上的牛的体重的和减去s 求最稳的罗汉倒下去风险的最大值 思路: 将s+w最大的放在下面,从上往下看 解决问题的代码: #include& ...

  4. Python及其常用模块库下载及安装

    一.Python下载:https://www.python.org/downloads/ 二.Python模块下载:http://www.lfd.uci.edu/~gohlke/pythonlibs/ ...

  5. Leetcode 599.两个列表的最小索引总和

    两个列表的最小索引总和 假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示. 你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅. 如果答 ...

  6. 聊聊、SpringBoot 静态资源访问

    SpringBoot 1.X 版本和 SpringBoot 2.X 版本在静态资源访问上有一些区别,如果直接从 1.X 升级到 2.X 肯定是有问题的.这篇文章就来讲讲这方面问题,也是项目中的坑. 先 ...

  7. [oldboy-django][6其他]学习django网站推荐

    http://www.cnblogs.com/holbrook/archive/2012/02/19/2358704.html alex: http://www.cnblogs.com/alex371 ...

  8. SGX技术初探

    一.SGX技术背景 1.1 SGX技术定义 SGX全称Intel Software Guard Extensions,顾名思义,其是对因特尔体系(IA)的一个扩展,用于增强软件的安全性.这种方式并不是 ...

  9. 【转】UGUI之用脚本动态的改变Button的背景图片 和 颜色

    http://blog.csdn.net/u014771617/article/details/45102701 public Button button;void Start(){ColorBloc ...

  10. Mysql InnoDB事务

    http://www.cnblogs.com/benshan/archive/2013/01/19/2867244.html 事务的四个特性 1.原子性(atomicity)原子性是指整个数据库事务是 ...