python中shell执行知识点


Posted in Python onMay 06, 2020

os.system

system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果。这个方法比较适用于外部程序没有输出结果的情况。

import os
os.system('ls')

commands.getstatusoutput

使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个文件句柄,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:

  • commands.getstatusoutput(cmd) 返回(status, output)
  • commands.getoutput(cmd) 只返回输出结果
  • commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法

当需要得到外部程序的输出结果时,本方法非常有用。比如使用urllib调用Web API时,需要对得到的数据进行处理。os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()

import os
ls = os.popen('ls')
print ls.read()

commands.getstatusoutput

使用commands模块的getoutput方法,这种方法同popend的区别在于popen返回的是一个文件句柄,而本方法将外部程序的输出结果当作字符串返回,很多情况下用起来要更方便些。
主要方法:

  • commands.getstatusoutput(cmd) 返回(status, output)
  • commands.getoutput(cmd) 只返回输出结果
  • commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法
import commands
commands.getstatusoutput('ls -lt')   # 返回(status, output)

subprocess.call

根据Python官方文档说明,subprocess模块用于取代上面这些模块。有一个用Python实现的并行ssh工具—mssh,代码很简短,不过很有意思,它在线程中调用subprocess启动子进程来干活。

from subprocess import call
call(["ls", "-l"])
import shlex, subprocess
def shell_command(cmd, timeout) :
  data = {"rc":False, "timeout":False, "stdout":"", "stderr":""}
  try :
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    try:
      outs, errs = process.communicate(timeout=timeout)
      data["stdout"] = outs.decode("utf-8") 
      data["stderr"] = errs.decode("utf-8") 
      data["rc"] = True

    except subprocess.TimeoutExpired :
      process.kill()
      outs, errs = process.communicate()
      data["rc"] = False 
      data["stdout"] = outs.decode("utf-8") 
      data["stderr"] = "timeout"
      data["timeout"] = True 

  except Exception as e :
    data["rc"] = False 
    data["stderr"] = e 

  finally : 
    return data

到此这篇关于python中shell执行知识点的文章就介绍到这了,更多相关python shell 执行内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python和php通信乱码问题解决方法
Apr 15 Python
python并发编程之多进程、多线程、异步和协程详解
Oct 28 Python
Django中反向生成models.py的实例讲解
May 30 Python
python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能
Jul 04 Python
python文字转语音的实例代码分析
Nov 12 Python
python函数局部变量、全局变量、递归知识点总结
Nov 15 Python
对pytorch的函数中的group参数的作用介绍
Feb 18 Python
解决pyecharts运行后产生的html文件用浏览器打开空白
Mar 11 Python
浅谈python处理json和redis hash的坑
Jul 16 Python
Anaconda+spyder+pycharm的pytorch配置详解(GPU)
Oct 18 Python
Python django框架 web端视频加密的实例详解
Nov 20 Python
Python爬虫之自动爬取某车之家各车销售数据
Jun 02 Python
Python 通过监听端口实现唯一脚本运行方式
May 05 #Python
python2.7使用scapy发送syn实例
May 05 #Python
python 使用raw socket进行TCP SYN扫描实例
May 05 #Python
Python之变量类型和if判断方式
May 05 #Python
Python实现CAN报文转换工具教程
May 05 #Python
python TCP包注入方式
May 05 #Python
python构造IP报文实例
May 05 #Python
You might like
php xml-rpc远程调用
2008/12/19 PHP
应用开发中涉及到的css和php笔记分享
2011/08/02 PHP
在yii中新增一个用户验证的方法详解
2013/06/20 PHP
微信网页授权(OAuth2.0) PHP 源码简单实现
2016/08/29 PHP
Swoole实现异步投递task任务案例详解
2019/04/02 PHP
JS在IE和FF下attachEvent,addEventListener学习笔记
2009/11/26 Javascript
THREE.JS入门教程(4)创建粒子系统
2013/01/24 Javascript
jquery append 动态添加的元素事件on 不起作用的解决方案
2015/07/30 Javascript
jquery使用ul模拟select实现表单美化的方法
2015/08/18 Javascript
jquery实现可自动收缩的TAB网页选项卡代码
2015/09/06 Javascript
js实现字符串和数组之间相互转换操作
2016/01/12 Javascript
JavaScript如何实现组合列表框中元素移动效果
2016/03/01 Javascript
JS中setTimeout的巧妙用法前端函数节流
2016/03/24 Javascript
Angularjs自定义指令实现三级联动 选择地理位置
2017/02/13 Javascript
jQuery实现全选、反选和不选功能
2017/08/16 jQuery
在Create React App中使用CSS Modules的方法示例
2019/01/15 Javascript
JS实现数组深拷贝的方法分析
2019/03/06 Javascript
javascript中undefined的本质解析
2019/07/31 Javascript
微信小程序实现滑动翻页效果(完整代码)
2019/12/06 Javascript
Echarts实现多条折线可拖拽效果
2019/12/19 Javascript
Vue路由切换页面不更新问题解决方案
2020/07/10 Javascript
Python基于tkinter模块实现的改名小工具示例
2017/07/27 Python
Python中XlsxWriter模块简介与用法分析
2018/04/24 Python
Python中property函数用法实例分析
2018/06/04 Python
pytorch中tensor的合并与截取方法
2018/07/26 Python
利用Python将文本中的中英文分离方法
2018/10/31 Python
python实现AES加密与解密
2019/03/28 Python
为何人工智能(AI)首选Python?读完这篇文章你就知道了(推荐)
2019/04/06 Python
Python使用crontab模块设置和清除定时任务操作详解
2019/04/09 Python
详解python uiautomator2 watcher的使用方法
2019/09/09 Python
python使用pip安装SciPy、SymPy、matplotlib教程
2019/11/20 Python
一个基于canvas的移动端图片编辑器的实现
2020/10/28 HTML / CSS
自荐信怎么写呢?
2013/12/09 职场文书
2015年建筑工作总结报告
2015/05/04 职场文书
小学见习报告
2015/06/23 职场文书
Pytorch中Softmax和LogSoftmax的使用详解
2021/06/05 Python