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.