要获得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'

本文出自https://blog.csdn.net/konglongaa/article/details/80314545

转载于:https://blog.51cto.com/lookingdream/2313078

python之commands模块相关推荐

  1. python commands执行不连续_[Python] 利用commands模块执行Linux shell命令

    用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...

  2. python中commands模块

    commands模块是python的内置模块,他共有三个函数,使用help(commands)可以查看到. 注:在3.x版本总,getstatus()方法被移除,getoutput()和getstat ...

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

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

  4. python commands_python之commands模块

    要获得shell命令的输出只需要`cmd`命令就可以了, 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数: 1). co ...

  5. python commands用法_python之commands模块(执行Linux Shell命令)

    commands模块 用于执行Linux shell命令,要获得shell命令的输出只需要在后面参数写入('命令')就可以了. 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块c ...

  6. Python资料之commands模块

    commands模块是python的内置模块,他共有三个函数,使用help(commands)可以查看到. 注:在3.x版本总,getstatus()方法被移除,getoutput()和getstat ...

  7. python commands_python之返回状态commands模块

    需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands很容易做到以上的效果. commands.getstatusoutput(cmd) 返回一个元组(status,o ...

  8. python commands模块_python之commands和subprocess入门介绍(可执行shell命令的模块)

    一.commands模块 1.介绍 当我们使用Python进行编码的时候,但是又想运行一些shell命令,去创建文件夹.移动文件等等操作时,我们可以使用一些Python库去执行shell命令. com ...

  9. python commands.getoutput_Python3中用什么替换commands模块的getstatusoutput()

    先翻译一段文章:https://stackoverflow.com/questions/11344557/replacement-for-getstatusoutput-in-python-3 在最后 ...

最新文章

  1. OSChina 周二乱弹 ——在影楼工作的妹子,可追不?
  2. jiebaR中文分词,从入门到喜欢
  3. Python与用户的交互 ,格式化输出的三种方式
  4. 队列顺序结构C/C++实现(数据结构严蔚敏版)
  5. Maven项目在pom文件中引入lib下的第三方jar包并打包进去
  6. qsort函数应用大全
  7. 【清单】值得「等待」的12个指示加载状态的 js 库
  8. Myeclipse的standard、pro、spring、Blue、Bling版本功能差别
  9. 华为微型计算机b515,华为MateStation B515台式机曝光:五种配置
  10. const int *p与int *const p的区别
  11. .NetCore中的程序通过Docker在CentOS中部署
  12. intel hd3000 本 在 64位 ubuntu10.04 下 液晶亮度无法调节 的解决之道(亲测)
  13. 【神经网络】神经元模型和感知器
  14. c语言分形程序,C语言实现分形图形
  15. 蓝叠手机模拟器使用技巧
  16. win 如何生成ssh密钥
  17. 元宇宙:不透明面纱下的“康德主义”
  18. 不一样的类小草图形写法
  19. mac 命令行安装软件
  20. 1235813找规律第100个数_2013年全国中考数学规律探索试题汇编

热门文章

  1. squid启动失败的解决办法
  2. 新手探索NLP(三)
  3. spring aop实现log 日志跟踪
  4. RabbitMQ-1
  5. CSS的四种引入方式
  6. CSS 会被继承的属性
  7. 通过form表单请求servlet资源代码
  8. Android 软键盘按键监控
  9. [2009.08.09]博客园北京俱乐部活动暨《博客园精华集:Web标准之道》现场签售通知...
  10. 《LeetCode力扣练习》第62题 不同路径 Java