欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python如何通過subprocess調(diào)用adb命令詳解

 更新時間:2017年08月27日 15:27:21   作者:RustFisher  
python可以說是寫一些小腳本的利器語法簡單,做為最著名的就“膠水語言”用它來寫一些命令腳本非常的方便。下面這篇文章主要給大家介紹了關(guān)于Python如何通過subprocess調(diào)用adb命令的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。

前言

本文主要給大家介紹了關(guān)于使用Python通過subprocess調(diào)用adb命令,subprocess包主要功能是執(zhí)行外部命令(相對Python而言)。和shell類似。

換言之除了adb命令外,利用subprocess可以執(zhí)行其他的命令,比如ls,cd等等。

subprocess 可參考: https://docs.python.org/2/library/subprocess.html

在電腦上裝好adb工具,配置好adb的環(huán)境變量,先確保shell中可以調(diào)用adb命令。

代碼示例

Python2.7

類 Adb,封裝了一些adb的方法

import os
import subprocess
class Adb(object):
 """ Provides some adb methods """
 @staticmethod
 def adb_devices():
  """
  Do adb devices
  :return The first connected device ID
  """
  cmd = "adb devices"
  c_line = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0]
  if c_line.find("List of devices attached") < 0: # adb is not working
   return None
  return c_line.split("\t")[0].split("\r\n")[-1] # This line may have different format
 @staticmethod
 def pull_sd_dcim(device, target_dir='E:/files'):
  """ Pull DCIM files from device """
  print "Pulling files"
  des_path = os.path.join(target_dir, device)
  if not os.path.exists(des_path):
   os.makedirs(des_path)
  print des_path
  cmd = "adb pull /sdcard/DCIM/ " + des_path
  result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
  print result
  print "Finish!"
  return des_path
 @staticmethod
 def some_adb_cmd():
  p = subprocess.Popen('adb shell cd sdcard&&ls&&cd ../sys&&ls',
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  return_code = p.poll()
  while return_code is None:
   line = p.stdout.readline()
   return_code = p.poll()
   line = line.strip()
   if line:
    print line
  print "Done"

some_adb_cmd方法執(zhí)行一連串的命令。各個命令之間用&&連接。

接著是一個死循環(huán),將執(zhí)行結(jié)果打印出來。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論