使用 adb 时 Python subprocess.run 与 Windows cmd 提示符的不同行为
我试图理解为什么在 Windows 中通过 cmd 提示符运行 adb 时出现错误,但在 subprocess.run 中却没有得到它。
这就是我正在做的事情。我正在运行 adb -s 而不提供序列号或设备 ID。这会导致 adb 在 cmd 中抛出错误:
c:\platform-tools>adb -s shell ls
adb.exe: ls requires an argument
c:\platform-tools>adb -s
adb.exe: -s requires an argument
但是,如果我使用 subprocess.run 和 shell=False:
subprocess.run(['C:\\platform-tools\\adb.exe', '-s', '', 'shell',
ls'],capture_output=True)
我不会收到任何错误,并且它会执行 shell 命令 ls。
如果我在没有 shell 命令的情况下运行它,而只是使用“-s”,它就会像我自己运行 adb.exe 一样执行,并列出帮助文本
subprocess.run(['C:\\platform-tools\\adb.exe', '-s',''],capture_output=True)
subproccess.run 到底发生了什么,它知道忽略空参数?
更新: 我只是对这种差异感到好奇,因为我正在编写一个小帮助程序脚本,让用户选择是否在连接多个设备时输入序列号,否则默认为 ''
。我注意到 subprocess.run 似乎忽略了空参数,因此我可以在没有条件语句来包含/排除“-s”选项的情况下逃脱,并且始终将“-s”选项保留在那里。
I am trying to understand why I get an error when running adb through cmd prompt in Windows, but don't get it in subprocess.run.
Here is what I am doing. I am running adb -s without providing it a serial number or device id. This causes adb to throw an error in cmd:
c:\platform-tools>adb -s shell ls
adb.exe: ls requires an argument
c:\platform-tools>adb -s
adb.exe: -s requires an argument
However, if I use subprocess.run with shell=False:
subprocess.run(['C:\\platform-tools\\adb.exe', '-s', '', 'shell',
ls'],capture_output=True)
I get no errors and it executes the the shell command ls.
If I run it without a shell command and just '-s ' it simply executes as if I just ran adb.exe on it's own and lists the help text
subprocess.run(['C:\\platform-tools\\adb.exe', '-s',''],capture_output=True)
What exactly is going on with subproccess.run that it knows to ignore empty arguments?
Update:
I am just curious regarding this difference because I am writing a small helper script to let the user choose whether or not to enter a serial number if they have multiple devices attached otherwise it defaults to ''
. I noticed subprocess.run seem to ignore the empty arguments so I can get away without having a conditional statement to include/exclude the "-s" option and just always keep the "-s" option there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用序列号(
-s
)调用adb
等于shell
,然后ls
抱怨缺失参数与前者后来再次抱怨的差异完全相同
,即序列号
shell
不存在的设备。如果您没有指定真实的序列号,则不要使用
-s
。You are invoking
adb
with a serial number (-s
) equals toshell
and thenls
complains of a missing argumentIt is exactly the same as
with the difference that the former will later complain again that the device with serial number
shell
does not exist.If you are not specifying a real serial number then don't use
-s
.