Run shell commands in Python

Source: Internet
Author: User

From: http://stackoverflow.com/questions/89228/how-to-call-external-command-in-python

 

Here's a summary of the ways to call external programs and the advantages and disadvantages of each:

  1. os.system("some_command with args")
    Passes
    Command and arguments to your system's shell. This is nice because you
    Can actually run multiple commands at once in this manner and set up
    Pipes and input/output redirection. For example,
    os.system("some_command < input_file | another_command > output_file")

    However, while this is convenient, you have to manually handle
    Escaping of shell characters such as spaces, etc. On the other hand,
    This also lets you run commands which are simply shell commands and not
    Actually external programs.
    Http://docs.python.org/lib/os-process.html

  2. stream = os.popen("some_command with args")
    Will do the same thingos.system
    Could t that it gives you a file-like object that you can use to access
    Standard Input/Output for that process. There are 3 other variants
    Popen that all handle the I/O slightly differently. If you pass
    Everything as a string, then your command is passed to the shell; if you
    Pass them as a list then you don't need to worry about escaping
    Anything.
    Http://docs.python.org/lib/os-newstreams.html

  3. ThePopen
    Class ofsubprocess
    Module. This is intended as a replacementos.popen
    But has the downside of being slightly more complicated by using UE of being so comprehensive. For example, you 'd say
    print Popen("echo Hello World", stdout=PIPE, shell=True).stdout.read()

    Instead
    print os.popen("echo Hello World").read()

    But it is nice to have all of the options there in one uniied class instead of 4 different popen functions.
    Http://docs.python.org/lib/node528.html

  4. Thecall
    Function fromsubprocess
    Module. This is basically just likePopen
    Class and takes all of the same arguments, but it simply wait until
    Command completes and gives you the return code. For example:
    return_code = call("echo Hello World", shell=True)

    Http://docs.python.org/lib/node529.html

  5. The OS module also has all of the fork/exec/spawn functions that
    You 'd have in a C program, but I don't recommend using them directly.

I have also used the output after the commands. getoutput ("ls") command is executed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.