execute a shell script from a python script

Discussion in 'Python' started by spec, Jul 17, 2006.

  1. spec

    spec Guest

    Hi all, I know nothing about Python. What I need to do is to get a
    Python script to execute a local shell script. I do not need any
    output. What would be th eeasiest way to accomplish this?

    spec, Jul 17, 2006
    1. Advertisements

  2. Thomas Nelson, Jul 17, 2006
    1. Advertisements

  3. spec

    spec Guest

    Thanks, actually there are no args, is there something even simpler?


    spec, Jul 18, 2006
  4. Check out os.popen4 or the commands module.
    John McMonagle, Jul 18, 2006
  5. spec

    Simon Forman Guest

    you could try os.system()
    Execute the command (a string) in a subshell. This is implemented
    by calling the Standard C function system(), and has the same
    limitations. Changes to posix.environ, sys.stdin, etc. are not
    reflected in the environment of the executed command.

    On Unix, the return value is the exit status of the process encoded
    in the format specified for wait(). Note that POSIX does not specify
    the meaning of the return value of the C system() function, so the
    return value of the Python function is system-dependent.

    On Windows, the return value is that returned by the system shell
    after running command, given by the Windows environment variable
    COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always
    0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status
    of the command run; on systems using a non-native shell, consult your
    shell documentation.

    Availability: Macintosh, Unix, Windows.
    Simon Forman, Jul 18, 2006
  6. .
    [more detail]
    I'm concerned the follow-ups in this thread have been too subtle.
    Here is what you need to know: use system(). A model such as

    import os

    fulfills exactly the requirements the original poster described.
    Cameron Laird, Jul 18, 2006
  7. As described in the docs I pointed to before:
    Is the way to do it without args. I think it is simplest to learn the
    subprocess module because (quoting from the docs) this module intends
    to replace several other, older modules and functions, such as:
    This way you only need to learn one thing. Actually I would like to
    see some of these older functions deprecated.


    Thomas Nelson, Jul 18, 2006
  8. .
    A point worth repeating, and I salute your courtesy in doing so.
    I had realized neither the deprecation of these interfaces, nor
    the documentation to that effect, so I thank you for pointing
    them out.
    Cameron Laird, Jul 18, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.