使用 adb 时 Python subprocess.run 与 Windows cmd 提示符的不同行为

发布于 2025-01-19 20:25:33 字数 877 浏览 1 评论 0原文

我试图理解为什么在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2025-01-26 20:25:33

您正在使用序列号(-s)调用adb等于shell ,然后ls抱怨缺失参数

adb -s shell ls

与前者后来再次抱怨的差异完全相同

adb ls
adb: ls requires an argument

,即序列号shell不存在的设备。

如果您没有指定真实的序列号,则不要使用-s

You are invoking adb with a serial number (-s) equals to shell and then ls complains of a missing argument

adb -s shell ls

It is exactly the same as

adb ls
adb: ls requires an argument

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文