Python执行系统命令的方法 os.system(),os.popen(),commands
2016-03-06 13:30
736 查看
转载:http://blog.csdn.net/b_h_l/article/details/12654749
第一种:使用os.system()
第二种:使用os.popen()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出
第三种:使用commands
总结:第一种最常用,但有时需要掌握命令执行后的结果,此时我们可以采用第三种
第一种:使用os.system()
import os os.system('cat /etc/profile')
第二种:使用os.popen()
import os output = os.popen('cat /proc/cpuinfo') print output.read()
通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出
第三种:使用commands
import commands (status,output)=commands.getstatusoutput('cat /etc/profile') print status print output #===============linux中 output=commands.getoutput('cat /etc/profile') status=commands.getstatus('cat /etc/profile') #===============同样可以用window中 (status,output)=commands.getstatusoutput('dir') print status print output
总结:第一种最常用,但有时需要掌握命令执行后的结果,此时我们可以采用第三种
相关文章推荐
- python 抓取百度音乐
- 基于Python实现一个简单的银行转账操作
- python Pillow 的简单案例
- “笨办法学python”学习笔记-错误整理
- python logging 日志输出 学习笔记
- Python输出中文乱码问题
- python os 常用命令
- Python PostgreSQL Psycopg2
- Python---装饰器
- python测试微信中谁把你删除了
- vim+python+OpenCV学习七 : Sobel算子、Laplacian算子和Canny边缘检测
- python的简单介绍
- 用python爬虫来爬华科宿舍查电费
- python _列表
- python之禅
- Python class and function json
- python基础02
- sublime 常见错误 UnicodeDecodeError: 'gbk' codec can't decode bytes
- python -抽象那些事
- Python脚本的基本格式和文档字符串