os.popen() vs os.system() 
Author Message
 os.popen() vs os.system()

Newbie...

From what I've deciphered, the only differences between os.popen() and
os.system() are:

a) popen automatically starts a new process while system only does it if you
include an &

b) popen hooks onto stdout and stdin

??? If so, what's the use of os.system()?



Sun, 09 Jun 2002 03:00:00 GMT  
 os.popen() vs os.system()

shopwork.com>,

Quote:

> Newbie...

> From what I've deciphered, the only differences between os.popen() and
> os.system() are:

> a) popen automatically starts a new process while system only does it
> if you include an &

> b) popen hooks onto stdout and stdin

> ??? If so, what's the use of os.system()?

os.system is a standard call to the C standard function call system().
It doesn't allow you to catch the output of the program like os.popen
does.  os.popen is piped open so that you can capture in your python
scripts what the program outputs.  os.system simply calls the outside
program and sends it's contents to stdout which can be redirected to
a file in your shell.  Call os.system if your python program doesn't
need to capture anything from your outside program.

charlie

Sent via Deja.com http://www.deja.com/
Before you buy.



Sun, 09 Jun 2002 03:00:00 GMT  
 os.popen() vs os.system()

Quote:

> a) popen automatically starts a new process while system only does it if you
> include an &

Not true:
Quote:
>>> os.getpid()
3264
>>> os.system("echo $$")

3658

Quote:
> b) popen hooks onto stdout and stdin

True. With os.popen(), you can read and write like a pipe.
os.system() doesn't let you do this. It's just like the C system()
function, which use I also don't understand. Maybe it's just
easier?

regards,
Gerrit.

--
"Nature abhors a Vacuum"

  -- Brian Behlendorf on OSS (Open Sources, 1999 O'Reilly and Associates)
 12:00pm  up 55 min, 16 users,  load average: 0.00, 0.00, 0.00



Sun, 09 Jun 2002 03:00:00 GMT  
 os.popen() vs os.system()

Quote:


> > a) popen automatically starts a new process while system only does it if you
> > include an &

> Not true:
> >>> os.getpid()
> 3264
> >>> os.system("echo $$")
> 3658

!? You are replying to a newbie here ;-) Didn't quite understand the answer...

PS, thanks to all for the great responses from everyone *grovel,grovel* up to
now. Hopefully this (coupled with my trusty book & tutorials) will
eventually elevate me from my current newbie status so I can actually help out
in here. Basically all my questions are more "WhyTo"s rather than "HowTo"s, does
that make them more challenging? ;-)



Sun, 09 Jun 2002 03:00:00 GMT  
 os.popen() vs os.system()

Quote:



> > > a) popen automatically starts a new process while system only does it if you
> > > include an &

> > Not true:
> > >>> os.getpid()
> > 3264
> > >>> os.system("echo $$")
> > 3658

> !? You are replying to a newbie here ;-) Didn't quite understand the answer...

Sorry.

A process has a unique process ID, pid. If one starts a new process, that
process gets a new process id. "echo $$" is a shell command to echo
the process id to stdout.
So if 'os.system("echo $$")' differs from os.getpid(), you can conclude
that it's not the same process.

Understood?

regards,
Gerrit.

--
"The IETF motto is 'rough consensus and running code'"

  -- Scott Bradner (Open Sources, 1999 O'Reilly and Associates)
  3:26pm  up  4:21, 16 users,  load average: 0.07, 0.04, 0.05



Sun, 09 Jun 2002 03:00:00 GMT  
 os.popen() vs os.system()

Quote:

>Newbie...

>From what I've deciphered, the only differences between os.popen() and
>os.system() are:

>a) popen automatically starts a new process while system only does it if you
>   include an &

Under Unix both start a new process.  

The important difference (as I understand it) is

 popen lets you interact with the program by reading and writing pipes
 while it's running.

 system() is more of a batch mode thing, the program runs to
 completion, then you get the return status.

Quote:
>??? If so, what's the use of os.system()?

If you just want to run a shell command and don't need to
provide any stdin and don't care about stdout/stderr.  For
example, if you just want to copy a directory tree from one
place to another:

 status = os.system("cp -r srcdir /some/where/else")

If you want to start a program, send it input and/or watch its
output as it runs, then use popen().

--
Grant Edwards                   grante             Yow!  I just heard the
                                  at               SEVENTIES were over!! And
                               visi.com            I was just getting in touch
                                                   with my LEISURE SUIT!!



Sun, 09 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Using os.system and os.popen from pythonwin under Windows NT

2. os.system/os.popen ultrasparc

3. Blocking ctrl-c to os.popen() or os.system()

4. os.popen results diff when executed in browser vs DOS

5. os.popen results differ run by browser vs DOS prompt

6. OS/VS COBOL and z/OS Version 1 Release 3

7. z/OS Version 1 Release 3 and OS/VS COBOL

8. os.system() or os.execv() w/stdout redirect and PID tracking

9. os._exit(), os.kill(os.getpid(), 9)

10. Problem with any of os.system(), os.fork() & os.execp() and os.spawn()

11. sigchld vs. os.popen, os.command, etc.

12. SF Bug #602245: os.popen() negative error code IOError

 

 
Powered by phpBB® Forum Software