执行子进程失败

2024-04-27 02:53:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过Python调用一个带有几个参数的进程。执行批处理文件本身对我来说很好,但是将其转换为Python会让我尖叫。批处理文件的内容如下:

"C:\Program Files\bin\cspybat" "C:\Program Files\bin\armproc.dll" "C:\Program Files\bin\armjlink.dll" "C:\Documents and Settings\USER\Desktop\CAL\testing\Verification\FRT\Code\TC1\Output\Genericb\Debug\Exe\Gen.out" --download_only --backend -B "--endian=little" "--cpu=Cortex-M3" "--fpu=None" "-p" "C:\Program Files\CONFIG\debugger\ST\iostm32f10xxb.ddf" "--drv_verify_download" "--semihosting" "--device=STM32F10xxB" "-d" "jlink" "--drv_communication=USB0" "--jlink_speed=auto" "--jlink_initial_speed=32" "--jlink_reset_strategy=0,0" 

批处理文件运行的可执行文件名为cspybat。可执行文件的输出提供以下信息:All parameters after——后端are passed to the back end

还要注意,有些参数是字符串,有些不是。

溶液

这对我很有用:

    """ MCU flashing function""" 
params = [r"C:\Program Files\bin\cspy",
          r"C:\Program Files\bin\arpro.dll",
          r"C:\Program Files\bin\arjink.dll",
          r"C:\Documents and Settings\USER\Desktop\Exe\GenerV530b.out",
          "--download_only", "--backend", "-B", "--endian=little", "--cpu=Cort3", "--fpu=None", "-p", 
          r"C:\Program Files\CONFIG\debugger\ST\iostm32f10xxb.ddf",
           "--drv_verify_download", "--semihosting", "--device=STM32F10xxB", "-d", "jlink", "--drv_communication=USB0",
            "--jlink_speed=auto", "--jlink_initial_speed=32", "--jlink_reset_strategy=0,0" ]
print(subprocess.list2cmdline(params))
p = subprocess.Popen(subprocess.list2cmdline(params))

Tags: and文件参数bindownloadfilesparamsprogram
2条回答

首先,你不需要所有这些引语。所以把它们移走。当文件名有空格时,只需要在有文件名的参数周围加引号(愚蠢的是,Windows经常这样做)。

您的参数只是一个字符串列表,其中一些需要引号。因为Windows对路径分隔符使用非标准的\,所以对这些名称使用“raw”字符串。

params = [
    r'"C:\Program Files\Systems\Emb Work 5.4\arm\bin\mpr.dll"',
    r'"C:\Program Files\Systems\Emb Work 5.4\arm\bin\ajl.dll"',
    r'"C:\Documents and Settings\USER\Desktop\abc.out"',
    "--backend",
    "-B", 
    "--endian=little",
    "--cpu=Cortex",
    "--fpu=None",
    "-p",
    r'"C:\Program Files\unknown\abc.ddf"',
    "--drv_verify_download",
    "--semihosting",
    "--device=STM32F10xxB",
    "-d",
    "jjftk",
    "--drv_communication=USB0",
    "--speed=auto",
    "--initial_speed=32",
    "--reset_strategy=0,0"]

使用类似于

program = r'"C:\Program Files\Systems\Emb Work 5.4\common\bin\run"'
subprocess.Popen( [program]+params )

要在Windows中执行批处理文件,请执行以下操作:

from subprocess import Popen
p = Popen("batchfile.bat", cwd=r"c:\directory\containing\batchfile")
stdout, stderr = p.communicate()

如果不想执行批处理文件,而是直接从Python执行问题中的命令,则需要对Popen的第一个参数进行一些试验。

首先,第一个参数可以是字符串或序列。

所以你要么写:

p = Popen(r'"C:\Program Files\Systems\Emb Work 5.4\common\bin\run" "C:\Program Files\Systems\Emb Work 5.4\arm\bin\mpr.dll" ... ...', cwd=r"...")

或者

p = Popen([r"C:\Program Files\Systems\Emb Work 5.4\common\bin\run", r"C:\Program Files\Systems\Emb Work 5.4\arm\bin\mpr.dll", ...], cwd=r"...")
# ... notice how you don't need to quote the elements containing spaces

根据文件:

On Windows: the Popen class uses CreateProcess() to execute the child program, which operates on strings. If args is a sequence, it will be converted to a string using the list2cmdline() method. Please note that not all MS Windows applications interpret the command line the same way: list2cmdline() is designed for applications using the same rules as the MS C runtime.

所以如果你使用一个序列,它将被转换成一个字符串。我可能会先尝试使用一个序列,因为这样就不必引用包含空格的所有元素(list2cmdline()为您这样做)。

为了排除故障,我建议您将序列传递给subprocess.list2cmdline(),并检查输出。

编辑:

如果我是你,我会这样做:

a)创建一个简单的Python脚本(testparams.py),如下所示:

import subprocess
params = [r"C:\Program Files\Systems\Emb Work 5.4\common\bin\run.exe", ...]
print subprocess.list2cmdline(params)

b)从命令行(python testparams.py)运行脚本,将输出复制并粘贴到另一个命令行,按enter并查看发生了什么。

c)如果不起作用,则编辑python文件并重复操作,直到它起作用。

相关问题 更多 >