6

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?

6
  • echo is a linux command. Run this on a linux machine and it will work flawlessly. Commented Jun 7, 2012 at 14:01
  • @Flo it is also a Windows shell command. Commented Jun 7, 2012 at 14:06
  • @JoeFish Indeed. My mistake. Reading kindall's answer makes a lot of sense. I don't know why I jumped to say that. Maybe I wanted linux to be more exclusive. :P Commented Jun 7, 2012 at 14:09
  • @flo if it makes you feel any better, I thought the same at first but opened a Windows shell and tried it before posting :) Commented Jun 7, 2012 at 14:10
  • @JoeFish I'm on a linux box :) Commented Jun 7, 2012 at 14:13

1 Answer 1

12

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I tried it this way earlier and it works. However, the method listed in my original answer is the one listed on the Python website. Is this because they are referring to the Linux echo and not the Windows echo ?
echo is an external command on Linux, yeah (though some shells also have an internal version, for performance). I think whoever wrote that did not take Windows into account!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.