Python os.system 和 os.popen的区别
2015-06-21 15:52
399 查看
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
(2) os.popen
# 该方法不但执行命令还返回执行后的信息对象
popen(command [, mode='r' [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.
例如:
好处在于:将返回的结果赋于一变量,便于程序的处理。
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
>>> os.system('ls') 04101419778.CHM bash document media py-django video 11.wmv books downloads Pictures python all-20061022 Desktop Examples project tools
(2) os.popen
# 该方法不但执行命令还返回执行后的信息对象
popen(command [, mode='r' [, bufsize]]) -> pipe
Open a pipe to/from a command returning a file object.
例如:
>>>tmp = os.popen('ls *.py').readlines() >>>tmp Out[]: ['dump_db_pickle.py ', 'dump_db_pickle_recs.py ', 'dump_db_shelve.py ', 'initdata.py ', '__init__.py ', 'make_db_pickle.py ', 'make_db_pickle_recs.py ', 'make_db_shelve.py ', 'peopleinteract_query.py ', 'reader.py ', 'testargv.py ', 'teststreams.py ', 'update_db_pickle.py ', 'writer.py ']
好处在于:将返回的结果赋于一变量,便于程序的处理。
相关文章推荐
- Python ValueError: invalid literal for int() with base 10 实用解决方法
- 让Python代码更快运行的5种方法
- Python文件读取的3种方法及路径转义
- Python中type的构造函数参数含义说明
- Python base64编码解码实例
- Python中的sorted函数以及operator.itemgetter函数
- python的常见矩阵运算
- alex教学视频登陆接口
- bottle框架学习(六)之错误与重定向
- python数据处理之基本函数
- Python计算地图上两点经纬度间的距离
- 冒泡算法 SAS & Python
- Python 切片(slice)的扩展使用方法
- python解决数据预处理:将KDDCPU99数据格式转换成libsvm可读的格式
- Python 发送邮件
- 使用python的Paramiko模块登陆SSH
- python函数
- Python(2)(基本输入输出语句)
- Python_09-面向对象编程
- Python base64编码解码实例