Python cmd command call

Source: Internet
Author: User

About the python call cmd command:

There are two main ways of doing this:

The 1.python OS module.

The OS module invokes the cmd command in two ways: Os.popen (), Os.system (). are invoked with the current process.

Os.system is unable to get the return value. Execute the program below when the run is finished. Usage such as: Os.system ("ipconfig").

Os.popen with the return value, how to get the return value. Such as

P=os.popen (CMD)

Print P.read (). A string is obtained.

Both are invoked with the current process, which means that they are all blocked.

2. Piping subprocess module.

The operating principle produces child processes under the current process.

Sub=subprocess. Popen (cmd,shell=true,stdout=subprocess. PIPE)

Sub.wait ()

Print Sub.read ()

Python cmd command call

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.