当路径中有空格时adb命令时会出错的解决方案
以下是一个典型的场景:
In [51]: path = input('请将apk文件拖至此处:')
请将apk文件拖至此处:"C:\Users\xxxxx\Desktop\00 飞机大战\com.qiyi.video.apk"
In [52]: path
Out[52]: '"C:\\Users\\xxxxx\\Desktop\\00 飞机大战\\com.qiyi.video.apk"'
In [53]: path = path.replace('\"', '')
In [54]: path
Out[54]: 'C:\\Users\\xxxxx\\Desktop\\00 飞机大战\\com.qiyi.video.apk'
In [55]: cmd = 'adb install -t ' + path
In [56]: subprocess.run(cmd, shell=True)
Failed to stat 飞机大战\com.qiyi.video.apk: No such file or directory
Out[56]: CompletedProcess(args='adb install -t C:\\Users\\qbgao\\Desktop\\00 飞机大战\\com.qiyi.video.apk', returncode=1)
报错的原因就是找不到原因?为什么找不到?apk名称很标准,所以是"00 飞机大战"带空格的文件路径它没有识别出来,想到很多办法一直没有解决,后面终于想到的一个法子,算是解决了. os.chdir(), 改变工作目录就可了.
In [60]: filepath, file = os.path.split(path)
In [61]: filepath
Out[61]: 'C:\\Users\\xxxxx\\Desktop\\00 飞机大战'
In [62]: file
Out[62]: 'com.qiyi.video.apk'
In [63]: os.getcwd()
Out[63]: 'C:\\Users\\xxxxx\\Desktop'
In [65]: os.chdir(filepath)
In [66]: os.getcwd()
Out[66]: 'C:\\Users\\xxxxx\\Desktop\\00 飞机大战'
In [68]: cmd = 'adb install -t ' + file
In [69]: cmd
Out[69]: 'adb install -t com.qiyi.video.apk'
In [70]: subprocess.run(cmd, shell=True)
Success
Out[70]: CompletedProcess(args='adb install -t com.qiyi.video.apk', returncode=0)
看,做了以上的修改后,终于可以成功了.
以下是个完整的实例,将电脑上的apk安装在手机中(apk可以是一个独立的apk文件,也可以是在在文件夹内的多个文件), 且文件夹的目录可能是带有空格的.
def installapk():
try:
path = input('请将待安装apk或apk folder拖入Console中:').replace('\"', "")
if os.path.exists(path) is False:
print('文件路径不存在,请确认?!')
return
else:
# 如果拖入的是一个apk文件
if os.path.isfile(path) is True:
filepath, file = os.path.split(path)
if os.path.splitext(path)[1] != '.apk':
print('非apk文件,无法安装.')
return
os.chdir(filepath)
if file.count(' ') != 0:
file = '\"' + file + '\"'
cmdinstall = 'adb install -t ' + file
print(f'{getnowdatatime(2)} apk正在安装... ...')
startInstallTime = time.time()
if subprocess.run(cmdinstall).returncode == 0:
print('apk已安装成功!')
s_time = time.strftime('%y-%m-%d %H:%M:%S', time.localtime(startInstallTime))
print(f'{s_time}')
endInstallTime = time.time()
e_time = time.strftime('%y-%m-%d %H:%M:%S', time.localtime(endInstallTime))
print(f'{e_time}\n')
total_time = endInstallTime - startInstallTime
print(f'安装耗时:{total_time}')
else:
print('apk安装Fail, 请check是否已安装过.')
# 如果拖入的是一个文件夹
else:
okCount, failCount = (0, 0)
apklist = list()
for file in os.listdir(path):
# if os.path.splitext(file)[1] == '.apk':
if file.endswith('.apk'):
apklist.append(file)
print(f'有{len(apklist)}个apk待安装.')
os.chdir(path)
filepath, file = os.path.split(path)
for i in range(len(apklist)):
if apklist[i].count(' ') != 0:
apklist[i] = '\"' + apklist[i] + '\"'
cmdinstall = 'adb install -t ' + apklist[i]
print(f'当前{getnowdatatime(2)}正在安装第{i+1}个apk:{apklist[i]}')
startInstallTime = getnowdatatime()
if subprocess.run(cmdinstall).returncode == 0:
print(f'第{i+1}个apk已安装成功!')
endInstallTime = getnowdatatime()
print(f'安装开始时间:{startInstallTime}')
print(f'安装完成时间:{endInstallTime}\n')
okCount = okCount + 1
else:
failCount = failCount + 1
print('此apk安装fail, 请check是否已安装过.')
print(f'所有应用共{len(apklist)}个, 已成功安装: {okCount}个,fail: {failCount}个.')
except KeyboardInterrupt:
print('\n手动中止操作:KeyboardInterrupt')
except EOFError:
print('\n未知异常: EOFError')