前提:确保手机设备已经连接到电脑上面
说明:文件保存为clear_sdcard_files.py,则命令行终端执行python3 clear_sdcard_files.py即可。

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @author: xiaoxiao
# @date  : 2019/4/6

import subprocess

cmds = [
    "cd sdcard/DCIM",
    "ls -l",
    "rm -r *.mp4",
    "ls -l",
    "exit",#这是非常关键的,退出
]

def shell(cmds):
    obj = subprocess.Popen("adb shell", shell= True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    obj.communicate(("\n".join(cmds) + "\n").encode('utf-8'))
    # # 下面可以将执行结果输出
    # info = obj.communicate(("\n".join(cmds) + "\n").encode('utf-8'))
    # for item in info:
    #     if item:
    #         print(item.decode('utf-8'))

shell(cmds)

备注:如果要用 shell 实现,请看下篇:https://testerhome.com/topics/18767


↙↙↙阅读原文可查看相关链接,并与作者交流