我收到以下错误:
WindowsError: [Error 2] The system cannot find the file specified我的代码是:
subprocess.call(["<<executable file found in PATH>>"])Windows 7,64位。Python 3.x最新版本,稳定。
有什么想法吗?
谢谢,
发布于 2011-01-07 00:00:29
当命令是内置外壳时,在调用中添加一个shell=True。
例如,对于dir,您可以键入:
import subprocess
subprocess.call('dir', shell=True)引用documentation的话
只有当您想要执行的命令内置于外壳程序(例如,目录、或copy)时,才需要在Windows上指定
shell=True。您不需要shell=True来运行批处理文件或基于控制台的可执行文件。
发布于 2015-09-27 01:29:13
在Windows上,我相信subprocess module doesn't look in the PATH unless you pass shell=True是因为它在幕后使用了CreateProcess()。但是,如果您传递的参数可能来自程序外部,则shell=True可能会带来安全风险。要使subprocess能够找到正确的可执行文件,可以使用shutil.which。假设PATH中的可执行文件名为frob
subprocess.call([shutil.which('frob'), arg1, arg2])(这适用于Python 3.3及更高版本。)
发布于 2016-09-13 23:51:30
在Windows上,你必须通过cmd.exe调用。正如阿帕拉拉提到的,Windows命令是在cmd.exe中实现的,而不是作为单独的可执行文件。
例如:
subprocess.call(['cmd', '/c', 'dir'])/c告诉cmd运行以下命令
这比使用shell=True更安全,后者允许外壳注入。
https://stackoverflow.com/questions/3022013
复制相似问题