[CPyUG]怎样获取其他程序向终端输出的值呢 - Google 网上论坛
ning...@gmail.com ![]()
10-4-6
需要在python中调用外部程序,比如采用os.system()
而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
能否用python直接获取这个值呢?
xxm...@gmail.com ![]()
10-4-6
subprocess模块可以捕捉到,或者简单点用管道也行2010/4/6 ning luwen <ningluwen@gmail.com>:
> 需要在python中调用外部程序,比如采用os.system()
> 而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
> 能否用python直接获取这个值呢?
>> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-cn@googlegroups.com
> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
> 详情: https://groups.google.com/group/python-cn
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>--
Any complex technology which doesn’t come with documentation must be the best
available.
Leo Jay ![]()
10-4-6
2010/4/6 ning luwen <ningluwen@gmail.com>:
> 需要在python中调用外部程序,比如采用os.system()
> 而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
> 能否用python直接获取这个值呢?
>os.popen
--
Best Regards,
Leo Jay
机械唯物主义 : linjunhalida ![]()
10-4-6
用pipe.2010/4/6 ning luwen <ningluwen@gmail.com>- 显示引用文字 -
--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-cn@googlegroups.com
退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
详情: https://groups.google.com/group/python-cn
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
ning...@gmail.com ![]()
10-4-6
谢谢大家 ok了2010/4/6 Leo Jay <python.leojay@gmail.com>:
- 显示引用文字 -> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:python-cn@googlegroups.com
> 退订: http://tinyurl.com/45a9tb //针对163/qq邮箱:http://tinyurl.com/4dg6hc
> 详情: https://groups.google.com/group/python-cn
> 严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp
>> To unsubscribe, reply using "remove me" as the subject.
>
Zengming Zhang ![]()
10-4-6
锟斤拷锟角革拷锟斤拷锟揭伙拷锟斤拷锟斤拷影桑锟斤拷锟斤拷锟斤拷屑锟解到锟斤拷锟绞硷拷锟斤拷锟斤拷锟窖伙拷失锟斤拷
os.popen()锟斤拷锟斤拷岱碉拷锟揭伙拷锟�ile锟斤拷锟襟,讹拷取锟斤拷锟缴o拷
>>> import os
>>> command = 'ls'
>>> aa = os.popen(command)
>>> aa
<open file 'ls', mode 'r' at 0xb6ff6d90>
>>> bb = aa.read()
>>> print bb
amsn_received
bin
Data
Desktop
diary
>>>
On 04/06/2010 03:04 PM, ning luwen wrote:
谢谢锟斤拷锟�ok锟斤拷 2010/4/6 Leo Jay <python.leojay@gmail.com>:2010/4/6 ning luwen <ningluwen@gmail.com>:锟斤拷要锟斤拷python锟叫碉拷锟斤拷锟解部锟斤拷锟津,憋拷锟斤拷锟斤拷锟�s.system() 锟斤拷锟斤拷锟斤拷玫某锟斤拷锟侥斤拷锟街憋拷锟斤拷锟斤拷斩锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷c锟斤拷锟斤拷直锟斤拷printf锟斤拷 锟杰凤拷锟斤拷python直锟接伙拷取锟斤拷锟街碉拷兀锟�os.popen -- Best Regards, Leo Jay -- 锟斤拷锟斤拷: `python-cn`:CPyUG ~ 锟斤拷锟斤拷锟矫伙拷锟斤拷 | 锟斤拷锟斤拷:python-cn@googlegroups.com 锟剿讹拷: http://tinyurl.com/45a9tb //锟斤拷锟�63/qq锟斤拷锟斤拷:http://tinyurl.com/4dg6hc 锟斤拷锟斤拷: https://groups.google.com/group/python-cn 锟斤拷锟斤拷: 锟斤拷锟斤拷斜锟� 锟角伙拷锟斤拷锟斤拷! http://wiki.woodpecker.org.cn/moin/AskForHelpTo unsubscribe, reply using "remove me" as the subject.
--
Zengming Zhang (nicegiving@gmail.com)
ZCNI, ZheJiang University
HomePage: http://systemsbiozju.org/people/zzm
PT M. ![]()
10-4-7
PEP推荐使用subprocess替换os.pipe等2010/4/6 Zengming Zhang <nicegiving@gmail.com>还是给读者一个例子吧,以免有检测到本邮件的朋友会失望:
os.popen()函数会返回一个file对象,读取即可:
>>> import os
>>> command = 'ls'
>>> aa = os.popen(command)
>>> aa
<open file 'ls', mode 'r' at 0xb6ff6d90>
>>> bb = aa.read()
>>> print bb
amsn_received
bin
Data
Desktop
diary