python-uiautomator运行shell命令

一、不需要root时

1.os.system()

#ls /mnt/文件,不需要root

d=conn_device()
ex_out = os.popen('adb shell ls -l /mnt/')
out = ex_out.readlines()
print(out)

2. d.shell()

#ls /mnt/文件,不需要root

d=conn_device()
#列表写shell命令
out,exit_code = d.shell(["ls",'-l','/mnt/'],timeout=60)
print(out)
#直接写shell命令
out2,e1 = d.shell('ls -s /mnt/', timeout=60)
print(out2)

二、需要root时

1.os.system()

#ls下/mnt/usb_storage文件,需要root,用adb root 或 adb shell su

os.system('adb root')
ex_out = os.popen('adb shell ls -s /mnt/usb_storage')
out = size.readlines()
print(out)

2. d.shell()

#用d.shell执行命令,需要root时,root、su分次执行不可以

os.system('adb root')
d.shell('su')
output,exit1 = d.shell(' ls -s /mnt/usb_storage',timeout=60)
print(output) #du: No /mnt/usb_storage: Permission denied

#需要一次性切换root并执行命令 可用su -c 切换为root用户 并接后续指令

su -c CMD
output,e3 = d.shell('su -c ls -s /mnt/usb_storage',timeout=60)
print(output) #0	/mnt/usb_storage1

三、实战

判断以USB开头的文件大小,进而判断U盘是否挂载

os.system('adb root')
size = os.popen('adb shell du -h /mnt/usb_storage*')
size_n = size.readlines()
for i in size_n:
    if int(i[0]) > 0:
        print('U盘文件已挂载!')
        #return 1
    else:
        print('U盘文件未挂载!')
        #return 0
output,e5 = d.shell('su -c du -h /mnt/usb_storage*',timeout=60)
a = output.strip().split('\n') #去除字符串前后的空格str.strip()
for i in a:
    if int(i[0]) > 0:
        print('U盘文件已挂载!')
        #return 1
    else:
        print('U盘文件未挂载!')
        #return 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值