473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pipes like perl

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
Aug 23 '05 #1
10 1471
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('@')])"
Aug 23 '05 #2
Here's one technique I use to run an external command in a particular
module:

stdin, stdout, stderr = os.popen3(cmd)
stdin.close()
results = stdout.readlines()
stdout.close()
errors = stderr.readlines()
stderr.close()
if errors:
raise Exception(''.join(errors))

Maybe this will get you going in a better direction?

Aug 23 '05 #3
bruno modulix wrote:
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.
"""


but i need to check the success/failure of the external command *before*
closing the file!
Aug 23 '05 #4
infidel wrote:
Here's one technique I use to run an external command in a particular
module:

stdin, stdout, stderr = os.popen3(cmd)
stdin.close()
results = stdout.readlines()
stdout.close()
errors = stderr.readlines()
stderr.close()
if errors:
raise Exception(''.join(errors))

Maybe this will get you going in a better direction?


yeah thanks!

i translated as:

.....
import os
.....
CMD_STDIN, CMD_STDOUT, CMD_STDERR = \
os.popen3("*some_system_command*", "r")
if not CMD_STDERR.readlines():
...
*do_something*
...
for answer in CMD_STDOUT:
print answer,
...
*do_something_more*
...
else:
...
*do_something_else*
...
CMD_STDIN.close()
CMD_STDOUT.close()
CMD_STDERR.close()
.....

bye

max

Aug 23 '05 #5
max(01)* wrote:
infidel wrote:
Here's one technique I use to run an external command in a particular
module:

stdin, stdout, stderr = os.popen3(cmd)
stdin.close()
results = stdout.readlines()
stdout.close()
errors = stderr.readlines()
stderr.close()
if errors:
raise Exception(''.join(errors))

Maybe this will get you going in a better direction?


yeah thanks!

i translated as:

.....
import os
.....
CMD_STDIN, CMD_STDOUT, CMD_STDERR = \
os.popen3("*some_system_command*", "r")
if not CMD_STDERR.readlines():
...
*do_something*
...
for answer in CMD_STDOUT:
print answer,
...
*do_something_more*
...
else:
...
*do_something_else*
...
CMD_STDIN.close()
CMD_STDOUT.close()
CMD_STDERR.close()
.....


but... i see it doesn't work for some commands, like "man python" (it
gets stuck on the "if" line)...

how come?

Aug 23 '05 #6
max(01)* enlightened us with:
but i need to check the success/failure of the external command
*before* closing the file!


You can't, unless you have a more intimite knowledge of the command
involved. If you know, for instance, that any output on stderr means
an error, you can check for just that. Without knowledge of the
command and it's output, the only thing you can check on is the exit
code.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Aug 24 '05 #7
In article <i%********************@news4.tin.it>,
"max(01)*" <ma**@fisso.casa> wrote:
in perl i can do this: .... but i do not know how to do it in python, because "if *command*:" gives
syntax error.

moreover, if i use .... 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...


Just to address this last point -- if you're running 2.4,
you can get this through the subprocess module. With its
popen equivalent, something like
subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout

will raise an exception if the command is not found. The
command in this case would specified as an argv list, not
a shell command.

The basic problem is that you have to fork first, then
exec, and by the time the forked interpreter finds out
that the exec didn't work, its parent has gone on to
do the I/O it's expecting. I think subprocess gets
around that, on UNIX, with a trick involving an extra
pipe, that would work only on UNIX.

Donn Cave, do**@u.washington.edu
Aug 24 '05 #8
> but... i see it doesn't work for some commands, like "man python" (it
gets stuck on the "if" line)...


..readlines() won't return until it hits end-of-file, but the "man"
command waits for user input to scroll the content, like the "more" or
"less" commands let you view "pages" of information on a terminal.

Aug 24 '05 #9
many thanks to all the fellows who cared to answer!

bye

max
Aug 25 '05 #10
>>>>> "infidel" <sa***********@gmail.com> (i) wrote:
i> .readlines() won't return until it hits end-of-file, but the "man"
i> command waits for user input to scroll the content, like the "more" or
i> "less" commands let you view "pages" of information on a terminal.


man shouldn't wait for user input if its output is a pipe. Try man man|cat.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Aug 25 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Christian Hammers | last post by:
Hello I would like to call a unix shellscript from within a PHP script and - write data to its STDIN - read data from its STDOUT *and* STDERR - get its exit code afterwards proc_open seems...
5
by: Jeroen van der Ham | last post by:
Hello, During my exploration of Python and rewriting something from Perl to Python I ran into something odd. I want to use a pipe so that a log of a program goes into a Python script instead of...
6
by: calmar | last post by:
Hi all, I would like to use python for a replacement for some binutils. I would like to be able to pipe things into python. Actually I would not like writing a 'script' to handle the input, but...
1
by: Swaroop C H | last post by:
Is there a Python equivalent of Perl's IPC::Run module http://search.cpan.org/dist/IPC-Run/lib/IPC/Run.pm ] ? Thanks! -- Swaroop C H Blog: http://www.swaroopch.info Book:...
1
by: Brian Hann | last post by:
I am attempting to write an application that uses the expect binary to get info off a telnet session. At first this app was written in mod_perl under perl 5.005_03 using Expect.pm but we're moving...
1
by: trt. | last post by:
Got a problem piping command output to a perl script, in the test below the loop accepts the piped files but it also interferes with my STDIN! ===test============================= $ls -1 *.cfg |...
3
by: EdgarBM | last post by:
Hi, does anyone know any kind of class from the .NET framework which works like unix pipes? Thank you in advance, Edgar
7
by: webmaster | last post by:
Sorry if this sounds naive, but I need to know how to create a two-way pipe between my Objective-C MacOSX program and another process, like perl, for example. Is there a simple way to do this in C,...
3
by: ZhukovL | last post by:
I'm having some trouble implementing the handling of multiple pipes in a shell I'm writing. I was hoping someone could point me in the right direction because I really cant see where I'm going...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.