1

Python version: '2.6.5 (r265:79063, Oct 1 2012, 22:07:21) \n[GCC 4.4.3]'

I have this:

>>> ss = subprocess.call("ls -z", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 

How do I get the error message now ?

This does not work:

>>> for i in subprocess.PIPE: ... print i ... Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable 
1
  • 1
    Is there any reason you're using shell=True and "ls -z" instead of ["ls", "-z"]? It's not relevant to your problem, but generally, using the shell when you don't need to is a bad idea, as is passing command lines and hoping you got the escaping right so they get parsed into the list you had in mind. Commented Aug 8, 2013 at 22:53

1 Answer 1

1

That combo of options doesn't work very well. Communicate() will read stdout, stderr and wait for the program to terminate.

proc = subprocess.Popen("ls -z", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate() assert proc.returncode == 0, 'i am sad' print out print err 

If you want to read stdout line by line as the program runs, you can create your own thread to babysit stderr or pipe stderr to stdout.

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

3 Comments

I know I could Popen. I was wondering how would I do what I want to do with just subprocess.call ?
call() doesn't give you access to the pipes - its for cases where you want the user to see the output or you pipe to a file. its just not the right tool for the job if you want to process the output streams.
This doesn't explain the actual error. subprocess.PIPE is just a magic constant that tells the subprocess functions "create a new pipe and use it here" when you pass it. (It happens to be -1, but that's not important.) So, iterating that magic constant won't do anything useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.