繁体   English   中英

在python中启动adb命令

[英]Launch adb commands in python

我尝试在没有自定义模块的python中启动adb命令。

尝试:

process = subprocess.Popen('cmd.exe',   stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, shell=True)
process.stdin.write("adb shell uninstall com.q.q".encode("utf8"))
process.stdin.write("adb shell install C:\\...\\qwerty.apk".encode("utf8"))

但这不起作用。 代码完成无结果

无法使用您的确切命令进行测试,但是效果很好:

import subprocess

process = subprocess.Popen('cmd.exe', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None, shell=True)
o,e = process.communicate(b"dir\n")
print(o)

(我得到目录的内容)

因此,对于您的示例,发送命令时缺少行终止符。 不会将命令发送给cmd程序,在此之前管道已断开。

那会更好:

import subprocess

process = subprocess.Popen('cmd.exe',   stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write(b"adb shell uninstall com.q.q\n")
process.stdin.write(b"adb shell install C:\\...\\qwerty.apk\n")
o,e = process.communicate()

但这是一种非常奇怪的运行命令的方式。 只需使用check_call ,将args正确分割即可:

subprocess.check_call(["adb","shell","uninstall","com.q.q"])
subprocess.check_call(["adb","shell","install",r"C:\...\qwerty.apk"])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM