管道像perl [英] pipes like perl
问题描述
嗨。
in perl我可以这样做:
....
if(打开(MYPIPE," * some_system_command * |"))
{
...
* do_something *
...
while($ answer =< MYPIPE>)
{
print $ answer;
}
...
* do_something_more *
...
}
其他
{
...
* do_something_else *
...
}
....
但我不知道如何在python中执行此操作,因为if * command *: "给出了
语法错误。
此外,如果我使用
....
导入os
....
尝试:
MYPIPE = os.popen(" * some_system_command *," r" ;)
...
* do_something *
...
回答MYPIPE:
打印答案,
MYPIPE.close()
...
* do_something_more *
...
除外:
...
* do_something_else *
...
....
它不起作用,因为* do_something *和* do_something_more *是
总是被执行(好像是
MYPIPE = os.popen(" * some_system_command *"," r")
即使* some_system_command *没有,也不会引发任何异常
存在/工作......
任何帮助?
非常感谢
max
< blockquote> answer =< MYPIPE>)
{
print
answer;
}
...
* do_something_more *
...
}
else
{
...
* do_something_else *
...
}
....
但我不知道如何在python中执行此操作,因为if * command *:给出了
语法错误。
此外,如果我使用
....
导入os
....
尝试:
MYPIPE = os.popen(" * some_system_command *," r" ;)
...
* do_something *
...
回答MYPIPE:
打印答案,
MYPIPE.close()
...
* do_something_more *
...
除外:
...
* do_something_else *
...
....
它不起作用,因为* do_something *和* do_something_more *是
总是被执行(好像是
MYPIPE = os.popen(" * some_system_command *"," r")
即使* some_system_command *没有,也不会引发任何异常
存在/工作......
任何帮助?
非常感谢
max
max(01) *写道:hi。
(剪辑)
它不起作用,因为* do_something * ;和* do_something_more *总是被执行(看起来好像是MYPIPE = os.popen(" * some_system_command *"," r")
>即使* some_system_command *不存在/工作也不会引发任何异常......
任何帮助?
http://www.python.org/ doc / 2.4.1 / lib /...# os-newstreams
&q uot;""
命令的退出状态(以
wait()指定的格式编码)可用作close()的返回值
文件对象的方法,除了当退出状态为零(终止
没有错误)时,返回None。
" "
-
bruno desthuilliers
python -c" print''@''。join([''。 ''.join([w [:: - 1] for p in p.split(''。'')])for
p in''o****@xiludom.gro' '.split(''@'')])"
hi.
in perl i can do this:
....
if (open (MYPIPE, "*some_system_command* |"))
{
...
*do_something*
...
while ($answer = <MYPIPE>)
{
print $answer;
}
...
*do_something_more*
...
}
else
{
...
*do_something_else*
...
}
....
but i do not know how to do it in python, because "if *command*:" gives
syntax error.
moreover, if i use
....
import os
....
try:
MYPIPE = os.popen("*some_system_command*, "r")
...
*do_something*
...
for answer in MYPIPE:
print answer,
MYPIPE.close()
...
*do_something_more*
...
except:
...
*do_something_else*
...
....
it doesn''t work, since "*do_something*" and *do_something_more* are
always executed (it seems like
MYPIPE = os.popen("*some_system_command*", "r")
does not raise any exception even if *some_system_command* does not
exist/work...
any help?
thanks a lot
max
answer = <MYPIPE>)
{
answer;
}
...
*do_something_more*
...
}
else
{
...
*do_something_else*
...
}
....
but i do not know how to do it in python, because "if *command*:" gives
syntax error.
moreover, if i use
....
import os
....
try:
MYPIPE = os.popen("*some_system_command*, "r")
...
*do_something*
...
for answer in MYPIPE:
print answer,
MYPIPE.close()
...
*do_something_more*
...
except:
...
*do_something_else*
...
....
it doesn''t work, since "*do_something*" and *do_something_more* are
always executed (it seems like
MYPIPE = os.popen("*some_system_command*", "r")
does not raise any exception even if *some_system_command* does not
exist/work...
any help?
thanks a lot
max
max(01)* wrote:hi.
(snip)
it doesn''t work, since "*do_something*" and *do_something_more* are
always executed (it seems like
MYPIPE = os.popen("*some_system_command*", "r")
does not raise any exception even if *some_system_command* does not
exist/work...
any help?
http://www.python.org/doc/2.4.1/lib/...#os-newstreams
"""
The exit status of the command (encoded in the format specified for
wait()) is available as the return value of the close() method of the
file object, except that when the exit status is zero (termination
without errors), None is returned.
"""
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"
这篇关于管道像perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!