2

I am trying to execute a command in python and read its output on command line in windows.

I have written the following code so far:

def build():
    command = "cobuild archive"
    print "Executing build"
    pipe = Popen(command,stdout=PIPE,stderr=PIPE)
    while True:     
        line = pipe.stdout.readline()
        if line:
            print line

I want to execute the command cobuild archive in command line and read it's output. However, the above code is giving me this error.

 File "E:\scripts\utils\build.py", line 33, in build
   pipe = Popen(command,stdout=PIPE,stderr=PIPE)
 File "C:\Python27\lib\subprocess.py", line 679, in __init__
   errread, errwrite)
 File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
   startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
2
  • 1
    You need to indent your code Commented Nov 19, 2013 at 19:02
  • What happens when you enter that command in the shell? Commented Nov 20, 2013 at 1:43

3 Answers 3

2

The following code worked. I needed to pass shell=True for the arguments

def build():    
command = "cobuild archive" 
pipe = Popen(command,shell=True,stdout=PIPE,stderr=PIPE)    

while True:         
    line = pipe.stdout.readline()
    if line:            
        print line
    if not line:
        break
Sign up to request clarification or add additional context in comments.

Comments

1

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

This error says that the subprocess module is unable to locate your executable(.exe)

here "cobuild archive"

Suppose, if your executable in this path: "C:\Users\..\Desktop", then, do,

import os

os.chdir(r"C:\Users\..\Desktop")

and then use your subprocess

6 Comments

Using output = commands.getstatusoutput(cmd) is giving me (1, "'{' is not recognized as an internal or external command,\noperable program or batch file.")
Also the batch file cobuild archive isn't triggered.
is your cobuild archive is an executable ?, to check this try running same from the command prompt (in the same directory), if its still throwing error, the problem is with your executable !!!
Yes. If I just run cobuild archive from the command prompt, it runs fine. I have been using that command from command line for a long time now. The executable isn't the problem. Its my python code.
Then in your subprocess call try, cmd = ["cobuild", "archive"], and pass this cmd to your subprocess
|
1

Do you mind to post your code with the correct indentations please? They have a large effect in python - another way of doing this is:

import commands
# the command to execute
cmd = "cobuild archive"
# execute and get stdout
output = commands.getstatusoutput( cmd )
# do something with output
# ...

Update:

The commands module has been removed in Python 3, so this is a solution for python 2 only.

https://docs.python.org/2/library/commands.html

2 Comments

The problem is not with the console read, but the execution itself
Please post comments like "Do you mind to post your code with the correct indentations please?" as a comment, or just edit the original post - thanks!

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.