免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3055 | 回复: 8
打印 上一主题 下一主题

关于os.system()一问。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-18 15:40 |只看该作者 |倒序浏览
我怎么得到os.system()命令输出的内容?

例如os.system('ls -l')

谢谢!

论坛徽章:
0
2 [报告]
发表于 2006-07-18 16:05 |只看该作者
6.8 subprocess -- Subprocess management

http://www.python.org/doc/2.4/lib/module-subprocess.html

论坛徽章:
0
3 [报告]
发表于 2006-07-21 16:56 |只看该作者
用commands模块吧

import commands

status, output = commands.getstatusoutput('ls -l')

论坛徽章:
0
4 [报告]
发表于 2006-07-21 21:46 |只看该作者
原帖由 MingStar@CU 于 2006-7-21 16:56 发表
用commands模块吧

import commands

status, output = commands.getstatusoutput('ls -l')


用用commands模块试了下,我的例子如下:

  1. #!d:/apptools/Python/python
  2. # filename useCMD.py
  3. # just list the system command for use in python mode

  4. import commands
  5. status, output = commands.getstatusoutput('cat -A')
复制代码

我的系统是MS XP, python2。4。3。怎么没有显示呀?

论坛徽章:
0
5 [报告]
发表于 2006-07-21 23:29 |只看该作者
原帖由 unixer2i 于 2006-7-21 21:46 发表


用用commands模块试了下,我的例子如下:
[code]
#!d:/apptools/Python/python
# filename useCMD.py
# just list the system command for use in python mode

impo ...



原因是commands模块只能在Unix(或Linux)里用~

详见:http://docs.python.org/lib/module-commands.html

论坛徽章:
0
6 [报告]
发表于 2006-07-21 23:49 |只看该作者
原帖由 guotie 于 2006-7-18 16:05 发表
6.8 subprocess -- Subprocess management

http://www.python.org/doc/2.4/lib/module-subprocess.html


查了一下,subprocess是打算用来代替os.system, os.popen, commands.* 等~

而楼主想要的结果用subprocess要这样子写:

  1. >>> import subprocess
  2. >>> outPipe = subprocess.Popen(["dir", '/w'], shell=True, stdout=subprocess.PIPE).stdout
  3. >>> output = outPipe.read()
复制代码


不过在Linux里的话,commands.getstatusoutput 就够用了。

论坛徽章:
0
7 [报告]
发表于 2006-07-22 19:51 |只看该作者
thanks.

论坛徽章:
0
8 [报告]
发表于 2006-07-22 22:47 |只看该作者
原帖由 MingStar@CU 于 2006-7-21 23:29 发表



原因是commands模块只能在Unix(或Linux)里用~

详见:http://docs.python.org/lib/module-commands.html


嗯,又学一招。

论坛徽章:
4
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:192015亚冠之广州恒大
日期:2015-07-22 17:20:15
9 [报告]
发表于 2006-07-23 08:56 |只看该作者
6楼的正确 如果你是要得到结果就用popen() 好像很多基础的教材上都有讲吧

os.popen
Run a shell command and connect to its input or output streams
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP