你应该使用check_output,下面是我成功运行的代码。在from subprocess import check_output, CalledProcessError
from tempfile import TemporaryFile
def __getout(*args):
with TemporaryFile() as t:
try:
out = check_output(args, stderr=t)
return 0, out
except CalledProcessError as e:
t.seek(0)
return e.returncode, t.read()
# cmd is string, split with blank
def getout(cmd):
cmd = str(cmd)
args = cmd.split(' ')
return __getout(*args)
def bytes2str(bytes):
return str(bytes, encoding='utf-8')
def isAdbConnected():
cmd = 'adb devices'
(code, out) = getout(cmd)
if code != 0:
print('something is error')
return False
outstr = bytes2str(out)
if outstr == 'List of devices attached\n\n':
print('no devices')
return False
else:
print('have devices')
return True
呼叫isAdbConnected()检查设备是否已连接。希望能帮助你。在