Subprocess in Python. Popen executes the command and continues to fetch the return value __python

Source: Internet
Author: User

Turn from: http://blog.sina.com.cn/s/blog_44d19b500102x21i.html


Let's start with an Android query connection device command to see how Subprocess.popen in Python is written. The command used for the ADB devices.

Import subprocess

order= ' adb devices ' #获取连接设备

Pi= subprocess. Popen (order,shell=true,stdout=subprocess. PIPE)

Print Pi.stdout.read () #打印结果


The result of the command return is one-time, so we read the data is not a problem, but there are some in the ADB command return the results in real time, such as the output of the mobile phone log command Logcat, the result will continue to print out the current device operation log information content, This type of command if we need to get the print result in Python, if we use the Read method, the wait result return time will be very long, here we have to change a method to read the results, written as follows.

Import subprocess

order= ' adb logcat '

Pi= subprocess. Popen (order,shell=true,stdout=subprocess. PIPE)

For I in ITER (Pi.stdout.readline, ' B '):

Print I


This kind of printing effect, like cmd in the same operation, real-time print out the log information. Here we use the ReadLine method and the ITER () function, which in fact is similar to the way we read files, single line reads, and all content reads.

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.