写在前面的一些P话:

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

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

核心代码很简单:

python学习交流群:660193417###
cmd = 'adb shell'
os.system(cmd)

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

Python学习交流Q群:660193417###
#!/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群: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()
##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应用集文件夹到这里就结束了,这不得给我一个赞~
当然,记得收藏起来慢慢学习,有问题的小伙伴记得评论留言,我看见都会回复的。

python删除Android应用及文件夹,就说牛不牛吧的更多相关文章

  1. 为什么 Android Studio 工程文件夹占用空间这么大?我们来给它减减肥

    偶然中发现Android Studio的工程文件夹比ADT Bundle的大很多.用Android Studio新建一个空工程,工程文件夹大小为30M,运行一次后大小为40M.同样用ADT Bundl ...

  2. Android 项目中文件夹的说明与作用(转)

    (转自:http://blog.csdn.net/goodshot/article/details/11529731) Android 项目中文件夹的作用 1. src:存放所有的*.java源程序. ...

  3. python之对指定目录文件夹的批量重命名

    python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...

  4. [转载]删除所有的.svn文件夹

    Windows 下,在DOS窗口中运行如下命令 dos 代码 for /r <你项目的路径> %i in (.svn) do rd /s /q %i Linux 下,可以先运行 显示出当前 ...

  5. 用Python来实现列举某个文件夹内所有的文件列表

    用Python来实现列举某个文件夹内所有的文件列表.吾八哥我动手写代码之前分析了下,遍历一个文件夹,肯定是需要用到os模块了,查阅模块帮助信息,可知os.listdir()方法可以列举某个文件夹内的所 ...

  6. 用bat批处理程序通过DOS命令行删除所有的空文件夹

    用过gothub或者码云的同学都知道,不包含任何文件的空文件夹上传提交时不被允许的.当然你可以在空文件下创建.keep文件(或.gitkeep文件),然后就可以上传了. 但是如果空文件夹比较多,并且我 ...

  7. window下删除所有带.svn文件夹及文件,删除所有的.svn文件夹

    (一)------------------------------------------------------------------------------------------------- ...

  8. Android Studio:layout-sw600dp文件夹中创建activity_main.xml

    1.右键res文件夹,新建Android resource directory文件夹 2.在resource type中选择layout  3.将Directory name命名为layout-sw6 ...

  9. 为什么 Android Studio 工程文件夹占用空间这么大?

    为什么 Android Studio 工程文件夹占用空间这么大? 学习了: https://www.cnblogs.com/chengyujia/p/5791002.html

随机推荐

  1. RecyclerView + SQLite 简易备忘录-----中(2)

    (3)RecyclerView的实现 ---中间的内容 RecyclerView是一个比ListView更加强大的滚动控件.要使用这个控件需要先在项目的build.gradle中添加RecyclerV ...

  2. Linux用命令设置终端背景色和字体颜色

    用命令改 1.setterm -inversecreen on 背景字体颜色互换 2.setterm -inversecreen on 恢复默认 3.setterm -[选项] [参数] |-back ...

  3. SerialPort-4.0.+ 使用说明(Kotlin版本)

    SerialPort-4.0.+ 项目官网 Java版本使用说明 介绍 SerialPort 是一个开源的对 Android 蓝牙串口通信的轻量封装库,轻松解决了构建自己的串口调试APP的复杂程度,让 ...

  4. linux 下通过fork实现后台运行进程

    1 # 通常建议使用双fork方法.在每个fork处,父级退出,子级继续 2 3 #!/usr/bin/env python 4 5 import time,platform 6 7 import o ...

  5. 增删改查- 万能map- 模糊查询

    1.编写接口 2.编写对应的mapper种的sql语句 3.测试 接口 public interface UserDao { List<User> getUserList(); //根据I ...

  6. c/c++递归打印文件夹

    调用linux的系统函数,实现tree的功能,递归打印文件夹 使用到得函数: DIR *opendir(const char *name); // 打开文件夹 struct dirent *readd ...

  7. 基于Docker&Kubernetes构建PaaS平台基础知识梳理

    点击上方"开源Linux",选择"设为星标" 回复"学习"获取独家整理的学习资料! 基于Docker&Kubernetes构建Paa ...

  8. Blazor和Vue对比学习(基础1.4):事件和子传父

    Blazor和Vue的组件事件,都使用事件订阅者模式.相对于上一章的组件属性,需要多绕一个弯,无论Blazor还是Vue,都是入门的第一个难点.要突破这个难点,一是要熟悉事件订阅模式<其实不难& ...

  9. 消息队列,IPC机制(进程间通信),生产者消费者模型,线程及相关

    消息队列 创建 ''' Queue是模块multiprocessing中的一个类我们也可以这样导入from multiprocessing import Queue,创 建时queue = Queue ...

  10. JS 的立即执行函数

    JS 的立即执行函数 本文写于 2019 年 12 月 7 日 其实 ES6 之后有了之后,很多之前的用法都没必要了,立即执行函数就是其一. 今天看到一道面试题: 请「用自己的语言」简述 立即执行函数 ...