python删除Android应用及文件夹,就是这么炫

前言

碌者劳其心力,懒人使用工具。程序员作为懒人推动社会进步,有目共睹。

adb 已提供了开发者可以使用的全部工具,但是重复执行一系列adb命令也令人心烦,所以,如果业务需求固定,直接在python脚

本执行adb命令。

在这里插入图片描述

核心代码很简单


cmd = 'adb shell'
 os.system(cmd)

1.业务需求: 执行应用卸载及删除指定目录

Python学习交流Q群:906715085###
#!/usr/bin/python
import subprocess
import os, sys
import getopt

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

if __name__ == '__main__':

  """ change commands and add shell"""
  
  tag = ''
  
  try:   
opt, args = getopt.getopt(sys.argv[1:], "ht:", ['pkg', 'help'])    
for op, value in opt:      
if op in ("-t", "--pkg"):        
tag = value      
if op in ("-h", "--help"):        
print "Usage: main_app_clean.py -t APP_PKG_NAME"        
print "Options:"        
print "  -t  APP_PKG_NAME should be a bundle id !"        
print ""        
print "Sample : ./main_app_clean.py -t <bundle id>"        
print ""        
sys.exit()  except getopt.GetoptError:              
print "Error: Could not find the args."            
print "Usage: main_app_clean.py -t APP_PKG_NAME"          
print "Options:"         
 print "  -t  APP_PKG_NAME should be a bundle id !"          
 print ""          
 print "Sample : ./main_app_clean.py -t <bundle id>"          
 print ""          sys.exit()
  if tag == '':    
  print "you should input a bundle id  !"    
  exit()  pkg = tag
  print ''  print '1) uninstalling ' + pkg +' ...'  unInstallCmd = 'adb uninstall  ' + pkg   os.system(unInstallCmd)
  print ''  print '2) cleaning the cached file...'  cleanCmd1 = 'adb shell rm -fR /sdcard/.DataBackupTest'  
  os.system(cleanCmd1)  cleanCmd2 = 'adb shell rm -fR /sdcard/.DataBackup'  os.system(cleanCmd2)  
  print ''  
  print '  All done !^_^!'  
  print ''
  exit()

使用方法:

  • 打开terminal
cd <uninstall_clean_app.py dir path>
  • 执行python命令
python ./uninstall_clean_app.py -t com.xxx.app

在这里插入图片描述

2.业务需求:推送obb文件到android设备并关联到对应的应用

Python学习交流Q群:906715085##3
#!/usr/bin/python
import subprocess
import os, sys
import getopt
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


if __name__ == '__main__':


  """ change commands and add shell"""
  
  path = ''  
  package = '' 
obbVersion = ''
#see: https://blog.csdn.net/chengxuyuanyonghu/article/details/54972854  
try:    
opt, args = getopt.getopt(sys.argv[1:], "hf:p:v:", ['file=', 'package=','obbVersion=','help'])    
for op, value in opt:      
if op in ("-f", "--file"):        
path = value     
 if op in ("-p", "--package"):        
package = value      
if op in ("-v", "--obbVersion"):        
obbVersion = value      
if op in ("-h", "--help"):        
print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion"        
print "Options:"        
print "  <-f> <-p> <-v> should not be null !"        
print ""        
print "OR: <--file=> <--package=> <-obbVersion=> should not be null "        
print ""        
print "Sample : ./obb_push.py  -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15"        
print ""        
print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15"        
print ""        
sys.exit()    
  
except getopt.GetoptError:            
print "Usage: obb_push.py -f obb/file/full/path -p com.example.app.bundleid -v app_vesion"           
print "Options:"           
print "  <-f> <-p> <-v> should not be null !"           
print "OR:"           
print " <--file=> <--package=> <-obbVersion=> should not be null "           
print ""           
print "Sample : ./obb_push.py  -f obb/file/full/path.obb -p <com.example.app.bundleid> -v 15"           
print ""           
print "OR : ./obb_push.py --file=obb/file/full/path.obb --package=com.example.app.bundleid --obbVersion=15"           
print ""           
sys.exit()
if path == '':    
print "you should input a obb file\'s path !"    
exit()
print '\n'  
print '||--------------------------------------------------------------||'  
print '\n'  
print 'NOTICE:'  
print 'obb file name rule: [main.bundleVersionCode.bundleID.obb]'  
print '\n'  
print '||--------------------------------------------------------------||'  
print '\n'
  print 'Start to copy obb file >>>>>>>>> '  
  print '  (1)===============> parsing obb file name:'  obbFilePath = path  
  if obbFilePath == '':    
  print 'you should input a obb file\'s path !'    
  exit()  
  obbSubDirs = obbFilePath.split('/')  # index  = len(obbSubDirs) - 1  obbFileName = obbSubDirs[-1]  print obbFileName  if obbFileName == '' or obbFileName.find('.obb') == -1:    
  print 'can not find a obb file in the path !'    
  exit()  
  print '\n'  
  print '    
  get package name = ' + package  
  print '\n'  
  print '  (3)===============> adb shell mkdir :'  obbDestPath = 'sdcard/Android/obb/' + package  subDir = ''  subDirs = obbDestPath.split('/')  for dir in subDirs:     
  subDir += '/' + dir     
  # print subDir      
  os.system('adb shell mkdir ' + subDir)
  print '\n'  print '  (4)===============> adb push obb file to device :'  pushCmd = 'adb push ' + obbFilePath.replace(' ','\\ ')+ ' /' + obbDestPath + '/'   
  # print pushCmd  os.system(pushCmd)
  print '\n'  
  print '  (5)===============> adb push rename obb file:'  
  newObbFileName = "main."+ obbVersion+"." + package + ".obb"  oldFileFullPath = '/' + obbDestPath + '/' + obbFileName   newFileFullPath = '/' + obbDestPath + '/' + newObbFileName 
   print '  old:' + oldFileFullPath  reameCmd = 'adb shell mv ' + oldFileFullPath + " " + newFileFullPath  os.system(reameCmd)  
  print '  new:' + newFileFullPath  print '\n'  print '  (6)===============> Completed!!!'  print '\n'  exit()

使用方法:

python </path/obb_push.py> -p <app package name > -f </path/of/obbfile> -v <app version code>

最后

今天给大家分享的删除Android应用集文件夹到这里就结束了,这不得给我一个赞。当然,记得收藏起来慢慢学习,有问题的小伙

伴记得评论留言,我看见都会回复的。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xff123456_/article/details/124425781