python中执行命令行的方法
2015-09-26 23:37
483 查看
1.os.system(command)
该方法和C语言里的system函数是一致的。该方法的参数就是string类型的命令,返回值方面,linux和windows上的返回值不同。linux上,返回值为执行命令的exit值;而windows上,返回值则是运行命令后,shell的返回值。注意,该方法是没办法得到运行命令的输出的。
2.os.popen
该方法返回的是file read的对象,如果想获取执行命令的输出,则需要调用该对象的read方法。
3.commands.getoutput(cmd), commands.getstatus, commands.getstatusoutput
这三个方法是一个系列的,入参是string类型的命令,出参分别是输出,exit值,元祖(exit值,输出)。这个系列的方法相对来说也是最方便的,尤其第三个,可以同时返回exit值和输出,非常方便。
该方法和C语言里的system函数是一致的。该方法的参数就是string类型的命令,返回值方面,linux和windows上的返回值不同。linux上,返回值为执行命令的exit值;而windows上,返回值则是运行命令后,shell的返回值。注意,该方法是没办法得到运行命令的输出的。
2.os.popen
该方法返回的是file read的对象,如果想获取执行命令的输出,则需要调用该对象的read方法。
3.commands.getoutput(cmd), commands.getstatus, commands.getstatusoutput
这三个方法是一个系列的,入参是string类型的命令,出参分别是输出,exit值,元祖(exit值,输出)。这个系列的方法相对来说也是最方便的,尤其第三个,可以同时返回exit值和输出,非常方便。
相关文章推荐
- python3.X 与其他语言小不同
- python-生成器
- python处理文件
- 生日悖论的Python实现
- Python游戏引擎开发(二):创建窗口以及重绘界面
- 20150926 kaggle Titanic入门篇excel&python.md
- 【Python】使用 multiprocessing.dummy 执行多线程任务
- python - bing_api_search
- 我的python学习笔记(2) - GDAL与Python
- Python 安装boto boto3
- mysql-python 安装错误: Cannot open include file: 'config-win.h': No such file or directory
- 学习python处理python编码问题
- python编码
- 使用 Python 连接 Caché 数据库
- 【python】自己编写的Set类
- Python __init.py__文件
- python 2 和 python 3
- Python变量和对象
- Python包管理pip工具在Mac OSX10.10上的安装及lxml安装错误的解决办法
- 用python发布一条微博