[CPyUG]怎样获取其他程序向终端输出的值呢

25 views
Skip to first unread message

ning luwen

unread,
Apr 6, 2010, 2:43:38 AM4/6/10
to pyth...@googlegroups.com
需要在python中调用外部程序,比如采用os.system()
而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
能否用python直接获取这个值呢?

xxmplus

unread,
Apr 6, 2010, 2:48:30 AM4/6/10
to pyth...@googlegroups.com
subprocess模块可以捕捉到,或者简单点用管道也行

2010/4/6 ning luwen <ning...@gmail.com>:


> 需要在python中调用外部程序,比如采用os.system()
> 而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
> 能否用python直接获取这个值呢?
>

> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@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

unread,
Apr 6, 2010, 2:49:18 AM4/6/10
to pyth...@googlegroups.com
2010/4/6 ning luwen <ning...@gmail.com>:

> 需要在python中调用外部程序,比如采用os.system()
> 而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
> 能否用python直接获取这个值呢?
>

os.popen

--
Best Regards,
Leo Jay

机械唯物主义 : linjunhalida

unread,
Apr 6, 2010, 2:49:19 AM4/6/10
to pyth...@googlegroups.com
用pipe.

2010/4/6 ning luwen <ning...@gmail.com>
需要在python中调用外部程序,比如采用os.system()
而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
能否用python直接获取这个值呢?

ning luwen

unread,
Apr 6, 2010, 3:04:01 AM4/6/10
to pyth...@googlegroups.com
谢谢大家 ok了

2010/4/6 Leo Jay <python...@gmail.com>:

> --
> 来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@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

unread,
Apr 6, 2010, 10:06:22 AM4/6/10
to pyth...@googlegroups.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
>>>

On 04/06/2010 03:04 PM, ning luwen wrote:
лл��� ok��

2010/4/6 Leo Jay <python...@gmail.com>:
  
2010/4/6 ning luwen <ning...@gmail.com>:
    
��Ҫ��python�е����ⲿ���򣬱������os.system()
������õij���Ľ��ֱ�����ն���� ������c����ֱ��printf��
�ܷ���pythonֱ�ӻ�ȡ���ֵ�أ�

      
os.popen

--
Best Regards,
Leo Jay

--
����: `python-cn`:CPyUG ~ �����û��� | ����:pyth...@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 (niceg...@gmail.com)
ZCNI, ZheJiang University
HomePage: http://systemsbiozju.org/people/zzm

PT M.

unread,
Apr 6, 2010, 10:11:07 PM4/6/10
to pyth...@googlegroups.com
PEP推荐使用subprocess替换os.pipe等

2010/4/6 Zengming Zhang <niceg...@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

>>>

On 04/06/2010 03:04 PM, ning luwen wrote:
谢谢大家 ok了

2010/4/6 Leo Jay <python...@gmail.com>:
  
2010/4/6 ning luwen <ning...@gmail.com>:
    
需要在python中调用外部程序,比如采用os.system()
而所调用的程序的结果直接向终端输出 (比如c里面直接printf)
能否用python直接获取这个值呢?

      
os.popen

--
Best Regards,
Leo Jay

--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@googlegroups.com
严正: 理解列表! 智慧提问! http://wiki.woodpecker.org.cn/moin/AskForHelp

To unsubscribe, reply using "remove me" as the subject.

    
--
Zengming Zhang (niceg...@gmail.com)
ZCNI, ZheJiang University
HomePage: http://systemsbiozju.org/people/zzm

--
来自: `python-cn`:CPyUG ~ 华蟒用户组 | 发言:pyth...@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



--
Arch Linuxer, Pythoner, Geek
--> Blog: http://apt-blog.net

阿暖

unread,
Apr 7, 2010, 7:54:57 PM4/7/10
to pyth...@googlegroups.com
subprocess在windows下总是有问题

On 4/7/10, PT M. <pen...@gmail.com> wrote:
> PEP推荐使用subprocess替换os.pipe等
>
> 2010/4/6 Zengming Zhang <niceg...@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
>>
>> >>>
>>
>> On 04/06/2010 03:04 PM, ning luwen wrote:
>>
>> 谢谢大家 ok了
>>

>> 2010/4/6 Leo Jay <python...@gmail.com> <python...@gmail.com>:
>>
>>
>> 2010/4/6 ning luwen <ning...@gmail.com> <ning...@gmail.com>:

Shane Yao

unread,
Apr 7, 2010, 10:12:46 PM4/7/10
to pyth...@googlegroups.com
有什么问题呢?能说一下吗?
Reply all
Reply to author
Forward
0 new messages