Windows无法在subprocess.call()上找到该文件

时间:2022-04-14 07:13:35

I am getting the following error:

我收到以下错误:

WindowsError: [Error 2] The system cannot find the file specified

My code is:

我的代码是:

subprocess.call(["<<executable file found in PATH>>"])

Windows 7, 64 bit. Python 3.x latest, stable.

Windows 7,64位。 Python 3.x最新,稳定。

Any ideas?

有任何想法吗?

Thanks,

谢谢,

5 个解决方案

#1


125  

I am not sure why but, on my windows machine I had to add a 'shell=True' to the call.

我不知道为什么,但是,在我的Windows机器上,我不得不在通话中添加'shell = True'。

E.g. for dir you would type:

例如。对于你会输入:

import subprocess
subprocess.call('dir', shell=True)

Hope this helps,

希望这可以帮助,

Douglas

道格拉斯

To quote from the documentation: The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

从文档中引用:在Windows上唯一需要指定shell = True的时候就是将要执行的命令内置到shell中(例如dir或copy)。您不需要shell = True来运行批处理文件或基于控制台的可执行文件。

#2


12  

On Windows, I believe the subprocess module doesn't look in the PATH unless you pass shell=True. However, shell=True can be a security risk if you're passing arguments that may come from outside your program. To make subprocess nonetheless able to find the correct executable, you can use shutil.which. Suppose the executable in your PATH is named frob:

在Windows上,我相信除非你传递shell = True,否则子进程模块不会查找PATH。但是,如果您传递的参数可能来自程序外部,则shell = True可能存在安全风险。为了使子进程能够找到正确的可执行文件,您可以使用shutil.which。假设PATH中的可执行文件名为frob:

subprocess.call([shutil.which('frob'), arg1, arg2])

(This works on Python 3.3 and above.)

(这适用于Python 3.3及更高版本。)

#3


7  

On Windows you have to call through cmd.exe. As Apalala mentioned, Windows commands are implemented in cmd.exe not as separate executables.

在Windows上,您必须通过cmd.exe进行调用。正如Apalala所提到的,Windows命令在cmd.exe中实现,而不是作为单独的可执行文件实现。

e.g.

例如

subprocess.call(['cmd', '/c', 'dir'])

/c tells cmd to run the follow command

/ c告诉cmd运行follow命令

This is safer than using shell=True, which allows shell injections.

这比使用shell = True更安全,它允许shell注入。

#4


3  

If the path has spaces, is it quoted?

如果路径有空格,是否引用?

And of course, you escaped backslashes properly, or used slashes, right?

当然,你正确地逃脱反斜杠,或使用斜杠,对吗?

#5


0  

If you are using powershell, then in it will be subprocess.call(['powershell','-command','dir']). Powershell supports a large portion of POSIX commands

如果你使用的是powershell,那么它将是subprocess.call(['powershell',' - command','dir'])。 Powershell支持大部分POSIX命令

#1


125  

I am not sure why but, on my windows machine I had to add a 'shell=True' to the call.

我不知道为什么,但是,在我的Windows机器上,我不得不在通话中添加'shell = True'。

E.g. for dir you would type:

例如。对于你会输入:

import subprocess
subprocess.call('dir', shell=True)

Hope this helps,

希望这可以帮助,

Douglas

道格拉斯

To quote from the documentation: The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. dir or copy). You do not need shell=True to run a batch file or console-based executable.

从文档中引用:在Windows上唯一需要指定shell = True的时候就是将要执行的命令内置到shell中(例如dir或copy)。您不需要shell = True来运行批处理文件或基于控制台的可执行文件。

#2


12  

On Windows, I believe the subprocess module doesn't look in the PATH unless you pass shell=True. However, shell=True can be a security risk if you're passing arguments that may come from outside your program. To make subprocess nonetheless able to find the correct executable, you can use shutil.which. Suppose the executable in your PATH is named frob:

在Windows上,我相信除非你传递shell = True,否则子进程模块不会查找PATH。但是,如果您传递的参数可能来自程序外部,则shell = True可能存在安全风险。为了使子进程能够找到正确的可执行文件,您可以使用shutil.which。假设PATH中的可执行文件名为frob:

subprocess.call([shutil.which('frob'), arg1, arg2])

(This works on Python 3.3 and above.)

(这适用于Python 3.3及更高版本。)

#3


7  

On Windows you have to call through cmd.exe. As Apalala mentioned, Windows commands are implemented in cmd.exe not as separate executables.

在Windows上,您必须通过cmd.exe进行调用。正如Apalala所提到的,Windows命令在cmd.exe中实现,而不是作为单独的可执行文件实现。

e.g.

例如

subprocess.call(['cmd', '/c', 'dir'])

/c tells cmd to run the follow command

/ c告诉cmd运行follow命令

This is safer than using shell=True, which allows shell injections.

这比使用shell = True更安全,它允许shell注入。

#4


3  

If the path has spaces, is it quoted?

如果路径有空格,是否引用?

And of course, you escaped backslashes properly, or used slashes, right?

当然,你正确地逃脱反斜杠,或使用斜杠,对吗?

#5


0  

If you are using powershell, then in it will be subprocess.call(['powershell','-command','dir']). Powershell supports a large portion of POSIX commands

如果你使用的是powershell,那么它将是subprocess.call(['powershell',' - command','dir'])。 Powershell支持大部分POSIX命令