system call in python

Discussion in 'Python' started by Catherine Yang, Aug 14, 2003.

  1. Hi,

    A question about how to execute a system command from python program. e.g.
    in a python script, I need to run another program written in C++, and after
    that come back to the same python program.

    Is it the system module, and which function do I use to do this?

    Thanks a lot
    Catherine
     
    Catherine Yang, Aug 14, 2003
    #1
    1. Advertising

  2. Catherine Yang wrote:

    >Hi,
    >
    >A question about how to execute a system command from python program. e.g.
    >in a python script, I need to run another program written in C++, and after
    >that come back to the same python program.
    >
    >Is it the system module, and which function do I use to do this?
    >
    >Thanks a lot
    >Catherine
    >
    >
    >
    >

    Probably you are looking for os.system, as in:

    import os
    cmd = 'ls -l /usr/bin'
    os.system(cmd)

    Note that this will not capture the output of the system command; you
    can use the os.popen family of commands to do that.

    -- Graham
     
    Graham Fawcett, Aug 14, 2003
    #2
    1. Advertising

  3. Hi,

    I think you can do the following:

    import commands
    commands.getstatusoutput("...your command to execture program...")

    SATISH


    On Thursday 14 August 2003 01:37 am, Graham Fawcett wrote:
    > Catherine Yang wrote:
    > >Hi,
    > >
    > >A question about how to execute a system command from python program. e.g.
    > >in a python script, I need to run another program written in C++, and
    > > after that come back to the same python program.
    > >
    > >Is it the system module, and which function do I use to do this?
    > >
    > >Thanks a lot
    > >Catherine

    >
    > Probably you are looking for os.system, as in:
    >
    > import os
    > cmd = 'ls -l /usr/bin'
    > os.system(cmd)
    >
    > Note that this will not capture the output of the system command; you
    > can use the os.popen family of commands to do that.
    >
    > -- Graham


    --
    SATISH K.CHIMAKURTHI
    GRAD. RESEARCH ASSISTANT
    UNIVERSITY OF KENTUCKY
    LEXINGTON
    KENTUCKY STATE
     
    satish k.chimakurthi, Aug 14, 2003
    #3
  4. Catherine Yang wrote:

    > Hi,
    >
    > A question about how to execute a system command from python program. e.g.
    > in a python script, I need to run another program written in C++, and after
    > that come back to the same python program.
    >
    > Is it the system module, and which function do I use to do this?
    >


    You can use the os.system() to execute a program accessible from your
    path or specified explicitly:

    >>> import os
    >>> os.system('ls')


    For more info on process control, take a look at:

    http://www.python.org/doc/current/lib/os-process.html
     
    Pierre Rouleau, Aug 14, 2003
    #4
  5. Re: Thanks a lot!

    Catherine Yang wrote:

    >Wow, you guys are *really fast and very helpful*. Thanks !!!!!!!
    >
    >

    We're merely trying to emulate our favourite language. ;-)

    -- Graham
     
    Graham Fawcett, Aug 14, 2003
    #5
  6. Graham Fawcett <> writes:

    > On an editorial aside, I'd nominate the "commands" module as a Python
    > wart.


    I seem to recall python-dev agrees with you...

    > It's not that the functions in this module aren't useful, but why
    > are they divorced from the very-much-related commands in the "os"
    > module?
    >
    > IIWG (If I were Guido) I think I would at least sub-package this
    > module as os.commands (in the spirit of os.path). While I was at it,
    > I'd probably do the same for shutil (--> os.shutil).


    Hmm. In a way, os is already too fat, I'm not sure *more* stuff
    should end up in there. If it was a real package, then maybe
    os.commands, os.net, os.shutil would make sense, but it's not.

    Occasionally people suggest that you should get at the standard
    library by doing something like

    from org.python import socket

    but then Guido shoots them :)

    > The Python standard library is full of dark corners bearing wondrous
    > mysteries, a Library of Alexandria full of useful code... but surely
    > even Alexandria's librarians put similar scrolls on the same shelf!


    This is what Alex Martelli is for :)

    Cheers,
    mwh

    --
    If I didn't have my part-time performance art income to help pay
    the bills, I could never afford to support my programming
    lifestyle. -- Jeff Bauer, 21 Apr 2000
     
    Michael Hudson, Aug 14, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    475
    Peter Hansen
    May 7, 2005
  2. system call and library call

    , Aug 22, 2005, in forum: C Programming
    Replies:
    7
    Views:
    3,017
    Jonathan Bartlett
    Aug 23, 2005
  3. leoman730

    System call and library call

    leoman730, May 3, 2007, in forum: C++
    Replies:
    6
    Views:
    917
    Jim Langston
    May 6, 2007
  4. David Pratt
    Replies:
    0
    Views:
    264
    David Pratt
    Apr 7, 2008
  5. David Pratt
    Replies:
    0
    Views:
    407
    David Pratt
    Apr 7, 2008
Loading...

Share This Page