Common methods for executing external programs in Python summary _python

Source: Internet
Author: User
Tags in python

The examples in this article summarize the common ways Python performs external programs. Share to everyone for your reference. The specific analysis is as follows:

In Python, we can invoke system commands or external programs directly in the following ways, using convenient

1, the Execl method of OS module

Python's execl system approach is consistent with UNIX's exec system calls. These methods apply to situations where an external program is invoked in a child process, because the external program replaces the code for the current process and does not return.
In other words, this shell process is occupied and will execute the first EXECL command program and no longer return.

2, using the OS module system method

The system method creates the child process to run the external program, which returns only the results of the external program's operation. This method is more suitable for cases where the external program has no output results. For example, under Ubuntu, use the command below to display a message on the desktop.
According to my experiment, the process called by the system method returns 0 at its normal end, and the end of the exception returns non 0, not depending on the return value of the process main function.

3, using the Popen method of OS module

This method is useful when you need to get output from an external program. For example, when using Urllib to invoke the Web API, the resulting data needs to be processed. One use example is as follows:

cmd = "ssh search47c.cm2 \" "+ Query +" \ "
#print cmd +" <br> "
output = Os.popen (cmd)
#对特殊字符进行转义
te MP1 = Output.read (). Replace (' < ', ' < ')
Temp2 = Temp1.replace (' > ', ' > ')
Temp3 = temp2.replace (' \ n ', " <br> ")
print temp3.replace ('/', '/")

4, using the Commands module GetOutput method (no use)

The difference between this method and Popend is that Popen returns a file handle, and this method returns the output of the external program as a string, which is more convenient in many cases.

I hope this article will help you with your Python programming.

Related Article

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.