Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: why does calling echo with subprocess return WindowsError 2?

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

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

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?

like image 659
user1442389 Avatar asked Jun 07 '12 13:06

user1442389


Video Answer


1 Answers

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)

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

like image 126
kindall Avatar answered Nov 14 '22 02:11

kindall