python 调用shell命令三种方法 分类: python基础学习 python Module 2014-01-13 14:13 239人阅读 评论(0) 收藏
2014-01-13 14:13
1161 查看
转载地址:http://hi.baidu.com/liheng_2009/item/28525511e5061a07b88a1a5e
1.用os.system(cmd) 不过取不了返回值
2.用os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()
使用 a.rstrip() 进行去除换行符“\n"
3.用 commands 模块。其实也是对popen的封装。此模块主要有如下方法
commands.getstatusoutput(cmd) 返回(status, output).
commands.getoutput(cmd) 只返回输出结果
commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法.如
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')'
-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls
1.用os.system(cmd) 不过取不了返回值
2.用os.popen(cmd) 要得到命令的输出内容,只需再调用下read()或readlines()等 如a=os.popen(cmd).read()
使用 a.rstrip() 进行去除换行符“\n"
3.用 commands 模块。其实也是对popen的封装。此模块主要有如下方法
commands.getstatusoutput(cmd) 返回(status, output).
commands.getoutput(cmd) 只返回输出结果
commands.getstatus(file) 返回ls -ld file的执行结果字符串,调用了getoutput,不建议使用此方法.如
>>> import commands
>>> commands.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')
>>> commands.getstatusoutput('cat /bin/junk')
(256, 'cat: /bin/junk: No such file or directory')
>>> commands.getstatusoutput('/bin/junk')
(256, 'sh: /bin/junk: not found')
>>> commands.getoutput('ls /bin/ls')
'/bin/ls'
>>> commands.getstatus('/bin/ls')'
-rwxr-xr-x 1 root 13352 Oct 14 1994 /bin/ls
相关文章推荐
- Python下调用Linux的Shell命令 分类: ubuntu python基础学习 2014-01-15 17:12 15631人阅读 评论(1) 收藏
- file()创建文件方法 分类: python python基础学习 2012-12-24 18:17 239人阅读 评论(0) 收藏
- open()文件对象的seek、tell方法详解 分类: python基础学习 open()文件读写 python Module 2013-09-05 09:40 450人阅读 评论(0) 收藏
- os.walk()方法 分类: python Module python基础学习 2013-08-09 07:59 342人阅读 评论(0) 收藏
- 继承父类,调用父类中的方法 分类: python 小练习 python基础学习 2014-01-10 17:54 325人阅读 评论(0) 收藏
- python调用shell总结 分类: ubuntu python基础学习 2014-02-26 14:32 261人阅读 评论(0) 收藏
- python之string模块 分类: python基础学习 python Module 2013-08-27 13:58 241人阅读 评论(0) 收藏
- sorted()排序详解 分类: python基础学习 python Module 2013-09-04 17:18 1372人阅读 评论(0) 收藏
- 字符串strip()方法注意事项 分类: python基础学习 2013-08-16 12:01 422人阅读 评论(0) 收藏
- 递归调用输出元素 分类: python 小练习 python基础学习 2013-06-30 11:54 278人阅读 评论(0) 收藏
- fileinput模块 分类: python基础学习 python 小练习 python Module 2013-08-15 17:18 417人阅读 评论(0) 收藏
- yield生成器对象返回Fiabs元素 分类: python 小练习 python基础学习 2014-01-13 10:28 193人阅读 评论(0) 收藏
- 静态方法 分类: python基础学习 2014-04-05 19:34 228人阅读 评论(0) 收藏
- vim python自动补全方法 分类: ubuntu python基础学习 虚拟机 测试 2013-07-05 15:37 603人阅读 评论(0) 收藏
- Python的静态方法和类成员方法 分类: python基础学习 2014-08-13 14:21 205人阅读 评论(0) 收藏
- python中的生成器(generator) 分类: python Module python基础学习 2013-10-28 17:41 310人阅读 评论(0) 收藏
- gzip 压缩格式的网站处理方法---sina.com 分类: python python基础学习 2013-07-16 17:40 362人阅读 评论(0) 收藏
- python string模块 分类: python基础学习 python Module 2013-08-16 17:50 294人阅读 评论(0) 收藏
- 使用生成器返回fibs列表 分类: python Module python基础学习 2013-10-28 18:19 283人阅读 评论(0) 收藏
- python变量赋值(可变与不可变) 分类: python基础学习 python Module 2013-10-30 11:59 1036人阅读 评论(0) 收藏