python:为什么用子进程调用echo会返回WindowsError 2? [英] python: why does calling echo with subprocess return WindowsError 2?

查看:40
本文介绍了python:为什么用子进程调用echo会返回WindowsError 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我有一个函数 runScript():

In my program, I have a function runScript():

def runScript():
subprocess.call(['echo', 'hello'])

我在 Python 文档中看到过很多类似的例子,所以我认为这会奏效.然而,当我在我的程序中调用这个函数时,它返回一个 WindowsError.

I have seen many similar examples in the Python documentation, so I assumed this would work. However, when I call this function in my program, it returns a WindowsError.

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

为什么会这样?我该如何解决?

Why does this happen? How can I fix it?

推荐答案

echo 命令内置于 Windows shell,cmd.exe.它不是可以在没有外壳的情况下调用的外部程序.因此,您的 subprocess.call() 需要指定 shell=True.

The echo command is built in to the Windows shell, cmd.exe. It is not an external program that can be called without the shell. Therefore, your subprocess.call() needs to specify shell=True.

subprocess.call('echo hello', shell=True)

(此外,shell 会为您处理拆分命令,所以我使用了更简单的单字符串样式来传递命令.)

(Also, the shell will handle splitting up the command for you, so I've used the simpler single-string style of passing the command.)

这篇关于python:为什么用子进程调用echo会返回WindowsError 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆