Python 调用 Shell命令
今天来总结一下
如果command有执行内容,会在标准输出显示。这实际上是使用C标准库函数system()实现的。
system方法会创建子进程运行外部程序,方法只返回外部程序的运行状态。这个方法比较适用于外部程序没有输出结果的情况
>>> os.system('ls')
1.py 51.29.txt dead.letter error.txt yue.csv
1.txt anaconda-ks.cfg dengji.sh kefuTongji.sh
0
1.py 51.29.txt dead.letter error.txt yue.csv
1.txt anaconda-ks.cfg dengji.sh kefuTongji.sh
>>> print a
0
2.os模块的popen方法
这个函数的返回值是一个文件对象,可以读或者写(由mode决定,mode默认是’r')。如果mode为’r',可以使用此函数的返回值调用read()来获取command命令的执行结果。
当需要得到外部程序的输出结果时,本方法非常有用,返回一个类文件对象,调用该对象的read()或readlines()方法可以读取输出内容。
>>>print a
3.commands模块的 getoutput 方法
主要方法:
* commands.getstatusoutput(cmd) 返回(status, output)
* commands.getoutput(cmd) 只返回输出结果
* commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议
>>> import commands
>>> commands.getstatusoutput('ls')
(0, '1.py\n1.txt\n2016_11_28_access_log\n51.29.txt\nanaconda-ks.cfg\ndata.txt\ndead.letter\ndengji.sh\ndiskJK,sh')
>>> commands.getoutput('ls')
'1.py\n1.txt\n2016_11_28_access_log\n51.29.txt\nanaconda-ks.cfg'
4.模块subprocess
subprocess意在替代其他几个老的模块或者函数,
比如:os.system os.spawn* os.popen* popen2.* commands.*
subprocess.call('脚本/shell', shell=True) 必须等待命令执行完毕
- p = subprocess.Popen("python stdTest.py",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
- sout = p.stdout.readlines()
- serr = p.stderr.readlines()
- print sout
- print serr
Python 调用 Shell命令的更多相关文章
- python 调用 shell 命令方法
python调用shell命令方法 1.os.system(cmd) 缺点:不能获取返回值 2.os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 ...
- python 调用shell命令三种方法
#!/usr/bin/python是告诉操作系统执行这个脚本的时候,调用/usr/bin下的python解释器: #!/usr/bin/env python这种用法是为了防止操作系统用户没有将pyth ...
- python 调用 shell 命令
记录 python 调用 shell 命令的方法 加载 os 模块, 使用 os 类 import os; os.system("ls /");
- 用Python调用Shell命令
Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如下几种方式: 第一种 ...
- python 调用shell命令的方法
在python程序中调用shell命令,是件很酷且常用的事情…… 1. os.system(command) 此函数会启动子进程,在子进程中执行command,并返回command命令执行完毕后的退出 ...
- python调用shell命令
1.subprocess介绍 官方推荐 subprocess模块,os.system(command) 这个废弃了 亲测 os.system 使用sed需要进行字符转义,非常麻烦 python3 su ...
- Python调用shell命令常用方法
Python调用shell指令 方法一.使用os模块的system方法:os.system(cmd),其返回值是shell指令运行后返回的状态码,int类型,0表示shell指令成功执行,256表示未 ...
- (转载)python调用shell命令之os 、commands、subprocess
linux系统下进入python交互式环境: 一.os 模块 1.1.os模块的exec方法簇: python交互界面中: In [1]: import os In [2]: os.exec os.e ...
- python调用shell命令之三慷慨法
preface: 忙于近期的任务,须要用到libsvm的一些命令.如在终端执行java svm_train train_file model_file. pythonsubset.py file tr ...
随机推荐
- vnpy源码阅读学习(4):自己写一个类似vnpy的UI框架
自己写一个类似vnpy的界面框架 概述 通过之前3次对vnpy的界面代码的研究,我们去模仿做一个vn.py的大框架.巩固一下PyQt5的学习. 这部分的代码相对来说没有难度和深度,基本上就是把PyQt ...
- 树dp 统计异或值
链接:https://ac.nowcoder.com/acm/contest/272/B来源:牛客网 题目描述 给定一棵n个点的树,每个点有权值.定义表示 到 的最短路径上,所有点的点权异或和. ...
- vue学习笔记3: 动态绑定
一.知识点 动态绑定: vue-class: 三目写法 对象写法 数组写法 vue-style: 三目写法 对象写法 数组写法 二.代码示例 1. vue-class vue-class三目写法 &l ...
- Oracle GoldenGate 12.3微服务架构指北
Microservices Architecture introduction Microservices Architecture is a method or approach to develo ...
- SuperSocket Code解析
SuperSocket1.6Code解析 Normal Socket System.Net.Sockets.dll程序集中使用socket类: 服务器: 创建socket:_socket = new ...
- mysql创建流水号
mysql数据库创建流水号 CREATE TRIGGER saledetail_id BEFORE INSERT ON saledetail FOR EACH ROW BEGIN declare n ...
- 强大的 Python 任务自动化工具!invoke 十分钟入门指南
接着前面的<tox 教程>,以及刚翻译好的<nox文档>,我们继续聊聊 Python 任务自动化的话题. nox 的作者在去年的 Pycon US 上,做了一场题为<Br ...
- 本地Linux虚拟机内网穿透,服务器文件下载到本地磁盘
本地Linux虚拟内网穿透 把服务器文件下载到本地磁盘 https://natapp.cn/ 1.注册账户点击免费隧道
- java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"
问题描述 今天在使用SpringBoot整合spring security,使用内存用户验证,但无响应报错:java.lang.IllegalArgumentException: There is n ...
- DirectX11 With Windows SDK--29 计算着色器:内存模型、线程同步;实现顺序无关透明度(OIT)
前言 由于透明混合在不同的绘制顺序下结果会不同,这就要求绘制前要对物体进行排序,然后再从后往前渲染.但即便是仅渲染一个物体(如上一章的水波),也会出现透明绘制顺序不对的情况,普通的绘制是无法避免的.如 ...