程序通过subprocess.Popen()调用时无法识别作为一个参数传递的两个参数。

huangapple go评论67阅读模式
英文:

Program invoked with subprocess.Popen() does not recognize two arguments passed as one

问题

我有一个在WSL2上运行的Python脚本中的函数,并且以超级用户身份运行它:

import subprocess

def flash() -> None:

    p = subprocess.Popen(
        ["JLinkExe", ""],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
        text=True
    )

    # 从JLinkExe读取响应
    response = p.stdout.read()
    print(response)

如你所见,我尝试运行JLinkExe程序,它是一个带有自己的Shell J-Link>的命令行实用程序。出于某种原因,我的print()函数输出如下:

SEGGER J-Link Commander V7.60(编译于2021年12月14日11:40:17)
DLL版本V7.60,编译于2021年12月14日11:40:00

未知的命令行选项。

前两行表明应用程序JLinkExe确实运行了,但由于某种原因,在参数的末尾附加了.,或者不识别空参数,这很奇怪。


如果我更改此行:

["JLinkExe", "-nogui 1"]

响应也会更改:

SEGGER J-Link Commander V7.60(编译于2021年12月14日11:40:17)
DLL版本V7.60,编译于2021年12月14日11:40:00

未知的命令行选项 -nogui 1。

再次出现.附加在参数的末尾...


如果我以超级用户身份直接在WSL中运行相同的命令,一切都正常:

$ sudo JLinkExe -nogui 1
SEGGER J-Link Commander V7.60(编译于2021年12月14日11:40:17)
DLL版本V7.60,编译于2021年12月14日11:40:00

通过USB连接到J-Link...O.K.
固件:J-Link STLink V21(编译于2019年8月12日10:29:20)
硬件版本:V1.00
S/N:775087052
VTref=3.300V

输入“connect”以建立目标连接,输入“?”以获取帮助
J-Link>

为什么我的参数未被识别?

英文:

I have this function in python script on WSL2 and I run it as a super user:

import subprocess

def flash() -> None:

    p = subprocess.Popen(
        ["JLinkExe", ""],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True,
        text=True
    )

    # Read the response from JLinkExe
    response = p.stdout.read()
    print(response)

As you can see I try to run program JLinkExe which is a command line utility with it's own shell J-Link>. For some reason my print() function prints this:

SEGGER J-Link Commander V7.60 (Compiled Dec 14 2021 11:40:17)
DLL version V7.60, compiled Dec 14 2021 11:40:00

Unknown command line option .

First two lines indicate that application JLinkExe did run, but for some reason there is a . appended at the back of the arguments or empty argument is not recognized which is weird.


If I change this line:

["JLinkExe", "-nogui 1"]

The response also changes:

SEGGER J-Link Commander V7.60 (Compiled Dec 14 2021 11:40:17)
DLL version V7.60, compiled Dec 14 2021 11:40:00

Unknown command line option -nogui 1.

Again . is appended to the back of the arguments...


If I run the same command as a super user directly in WSL everything looks fine:

$ sudo JLinkExe -nogui 1
SEGGER J-Link Commander V7.60 (Compiled Dec 14 2021 11:40:17)
DLL version V7.60, compiled Dec 14 2021 11:40:00

Connecting to J-Link via USB...O.K.
Firmware: J-Link STLink V21 compiled Aug 12 2019 10:29:20
Hardware version: V1.00
S/N: 775087052
VTref=3.300V

Type "connect" to establish a target connection, '?' for help
J-Link>

Why are my arguments not recognized?

答案1

得分: 1

没有将.附加到参数中,这是JLinkExe命令的输出,它是一个以.结尾的英语句子:

Unknown command line option <argument>.

&quot;&quot;&quot;-nogui 1&quot;都不是JLinkExe的有效参数,但&quot;-nogui&quot;是有效参数,它期望另一个参数&quot;1&quot;

你需要两个单独的参数:

["JLinkExe", "-nogui", "1"]

如果你只想调用JLinkExe而不传递任何参数,不要将空字符串作为参数传递:

["JLinkExe"]
英文:

There is no . appended to the arguments, that's the output of the JLinkExe command which is an English sentence which ends with a .:

Unknown command line option &lt;argument&gt;.

Neither &quot;&quot; nor &quot;-nogui 1&quot; are valid arguments for JLinkExe, but &quot;-nogui&quot; is, which expects another one, &quot;1&quot;.

You need two separate arguments:

[&quot;JLinkExe&quot;, &quot;-nogui&quot;, &quot;1&quot;]

If you want to invoke just JLinkExe with no arguments, don't pass an empty string as an argument:

[&quot;JLinkExe&quot;]

huangapple
  • 本文由 发表于 2023年7月14日 07:15:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683800.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定