1

I have Python 3.2 installed, and i am trying to use the subprocess module, but i keep getting errors.

The code i am using is :

import subprocess
subprocess.check_output(["echo", "Hello World!"])
subprocess.check_output("exit 1", shell=True)

i keep getting the following error for subprocess.check_output(["echo", "Hello World!"])

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    subprocess.check_output(["echo", "Hello World!"])
  File "C:\Python32\lib\subprocess.py", line 514, in check_output
    process = Popen(*popenargs, stdout=PIPE, **kwargs)
  File "C:\Python32\lib\subprocess.py", line 744, in __init__
    restore_signals, start_new_session)
  File "C:\Python32\lib\subprocess.py", line 977, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

and for the line subprocess.check_output("exit 1", shell=True) i get the following error:

Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    subprocess.check_output("exit 1", shell=True)
  File "C:\Python32\lib\subprocess.py", line 521, in check_output
    raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
1
  • The second one is absolutely clear: you tell the shell to exit with status 1, and you use check_output() which raises an exception upon non-zero exit codes. Commented Jun 25, 2013 at 15:32

1 Answer 1

2

As far as I can see, the problem in not with subprocess -- the problem is there is no executable named echo on the PATH:

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

My guess is echo is a Windows Shell internal command. Try to launch it with shell=True:

subprocess.check_output(["echo", "Hello World!"], shell=True)

If you want to call an executable program (".exe") you don't need a shell. Just pass the program's name and the optional arguments:

subprocess.check_output(["notepad.exe", "file.txt"])

BTW, what are you trying to do with the line:

subprocess.check_output("exit 1", shell=True)

This only launch a subshell, asking it to exit immediately with a non-zero status code ?!?

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

4 Comments

i was just following the example code they had on the python website, and thanks subprocess.check_output(["echo", "Hello World!"], shell=True) seems to work perfectly, Also do you know to call a command exe while passing in parameters
@Rohit I'm not accustomed to windows, but I have edited my answer to show you how to launch "notepad.exe". That should work. Let me know if it doesn't.
thanks sylvain, launching the notepad seems to work fine, but i am trying to call a command exe with some parameters, then i have to wait until it connects to some server, then pass in the login and the password, then pipe the output from the command exe to a file.
but the answer you provided was perfect for the question i asked. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.