Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2486 > unrolled thread

Get subprocess error output from shell command

Started byGnarlodious <gnarlodious@gmail.com>
First post2011-04-02 20:07 -0700
Last post2011-04-02 21:14 -0700
Articles 9 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Get subprocess error output from shell command Gnarlodious <gnarlodious@gmail.com> - 2011-04-02 20:07 -0700
    Re: Get subprocess error output from shell command Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-04-02 23:25 -0400
      Re: Get subprocess error output from shell command Gnarlodious <gnarlodious@gmail.com> - 2011-04-02 20:36 -0700
    Re: Get subprocess error output from shell command Chris Rebert <clp2@rebertia.com> - 2011-04-02 20:29 -0700
      Re: Get subprocess error output from shell command Gnarlodious <gnarlodious@gmail.com> - 2011-04-02 20:50 -0700
        Re: Get subprocess error output from shell command Benjamin Kaplan <benjamin.kaplan@case.edu> - 2011-04-03 00:03 -0400
        Re: Get subprocess error output from shell command Chris Rebert <clp2@rebertia.com> - 2011-04-02 21:10 -0700
      Re: Get subprocess error output from shell command Gnarlodious <gnarlodious@gmail.com> - 2011-04-02 21:00 -0700
        Re: Get subprocess error output from shell command Chris Rebert <clp2@rebertia.com> - 2011-04-02 21:14 -0700

#2486 — Get subprocess error output from shell command

FromGnarlodious <gnarlodious@gmail.com>
Date2011-04-02 20:07 -0700
SubjectGet subprocess error output from shell command
Message-ID<dc8e1dcd-303c-4e07-bdda-1b12277976f0@f31g2000pri.googlegroups.com>
I'm running a shell command like:
plutil -convert xml1 "~/Library/Preferences/iCab/iCab 4 Bookmarks"

Getting error:
~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied

How would I capture this error using a method of subprocess?

I read the doc at
http://docs.python.org/release/3.0.1/library/subprocess.html

but confess I don't understand it.

-- Gnarlie
http://Gnarlodious.com/

[toc] | [next] | [standalone]


#2488

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2011-04-02 23:25 -0400
Message-ID<mailman.149.1301801169.2990.python-list@python.org>
In reply to#2486
On Sat, Apr 2, 2011 at 11:07 PM, Gnarlodious <gnarlodious@gmail.com> wrote:
> I'm running a shell command like:
> plutil -convert xml1 "~/Library/Preferences/iCab/iCab 4 Bookmarks"
>
> Getting error:
> ~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied
>
> How would I capture this error using a method of subprocess?
>
> I read the doc at
> http://docs.python.org/release/3.0.1/library/subprocess.html
>
> but confess I don't understand it.
>
> -- Gnarlie
> http://Gnarlodious.com/
> --

Yeah, the subprocess docs are a bit confusing. Here's what you need to do.



from subprocess import Popen, PIPE

#create a Popen object
#note that I'm putting the command in a list
#the stdout=PIPE thing says I want to capture it
process = Popen( ['plutil', '-convert', 'xml1',
'~/Library/Preferences/iCab/iCab 4 Bookmarks'], stdout = PIPE, stderr
= PIPE)

#then, you communicate with the process

outdata, errdata = process.communicate()

[toc] | [prev] | [next] | [standalone]


#2490

FromGnarlodious <gnarlodious@gmail.com>
Date2011-04-02 20:36 -0700
Message-ID<24725b98-36b3-4a9e-a7fb-47052e3e5f4f@j9g2000prj.googlegroups.com>
In reply to#2488
OK I get it, and that seems like it should work. But when I simulate a
permissions error by setting the file to unwritable I get an error:

outdata, errdata = process.communicate()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/
python3.1/subprocess.py", line 715, in communicate
    return self._communicate(input)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/
python3.1/subprocess.py", line 1191, in _communicate
    stdout, stderr = self._communicate_with_poll(input)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/
python3.1/subprocess.py", line 1236, in _communicate_with_poll
    register_and_append(self.stdout, select_POLLIN_POLLPRI)
  File "/Library/Frameworks/Python.framework/Versions/3.1/lib/
python3.1/subprocess.py", line 1223, in register_and_append
    poller.register(file_obj.fileno(), eventmask)
ValueError: I/O operation on closed file


Is there a way to trap the last line and handle it instead of crashing
my webapp?

-- Gnarlie

[toc] | [prev] | [next] | [standalone]


#2489

FromChris Rebert <clp2@rebertia.com>
Date2011-04-02 20:29 -0700
Message-ID<mailman.150.1301801357.2990.python-list@python.org>
In reply to#2486
On Sat, Apr 2, 2011 at 8:07 PM, Gnarlodious <gnarlodious@gmail.com> wrote:
> I'm running a shell command like:
> plutil -convert xml1 "~/Library/Preferences/iCab/iCab 4 Bookmarks"
>
> Getting error:
> ~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied
>
> How would I capture this error using a method of subprocess?
>
> I read the doc at
> http://docs.python.org/release/3.0.1/library/subprocess.html
>
> but confess I don't understand it.

from subprocess import Popen, PIPE

target = '~/Library/Preferences/iCab/iCab 4 Bookmarks'
args = ['plutil', '-convert', 'xml1', target]

proc = Popen(args, stdout=PIPE, stderr=PIPE)
output, error_output = proc.communicate()

if proc.returncode: # non-zero exit status, indicating error
    print("Encountered error:")
    print(error_output) # output the error message

Cheers,
Chris
--
http://blog.rebertia.com

[toc] | [prev] | [next] | [standalone]


#2491

FromGnarlodious <gnarlodious@gmail.com>
Date2011-04-02 20:50 -0700
Message-ID<7d8d2159-20ac-4bdc-afa8-f28b8ee29394@d26g2000prn.googlegroups.com>
In reply to#2489
I get it, you instantiate an object, call a method and get a tuple in
response. However, here is what I see:

>>> process.communicate()
(b'~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied\n',
b'')

So all I get is the string and no error message, which is the same
thing I get with the simpler subprocess.call(). I can parse out the
error out and handle it if I need to. Is this a failing in the OSX
plutil tool?

-- Gnarlie

[toc] | [prev] | [next] | [standalone]


#2495

FromBenjamin Kaplan <benjamin.kaplan@case.edu>
Date2011-04-03 00:03 -0400
Message-ID<mailman.151.1301803423.2990.python-list@python.org>
In reply to#2491
On Sat, Apr 2, 2011 at 11:50 PM, Gnarlodious <gnarlodious@gmail.com> wrote:
> I get it, you instantiate an object, call a method and get a tuple in
> response. However, here is what I see:
>
>>>> process.communicate()
> (b'~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied\n',
> b'')
>
> So all I get is the string and no error message, which is the same
> thing I get with the simpler subprocess.call(). I can parse out the
> error out and handle it if I need to. Is this a failing in the OSX
> plutil tool?
>
> -- Gnarlie
> --

Were you expecting a Python error? That's not how POSIX shells work. A
process that fails just gives you a non-zero return code, not an
exception. You can call process.poll() to get the return code.


> http://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#2496

FromChris Rebert <clp2@rebertia.com>
Date2011-04-02 21:10 -0700
Message-ID<mailman.152.1301803824.2990.python-list@python.org>
In reply to#2491
On Sat, Apr 2, 2011 at 9:03 PM, Benjamin Kaplan
<benjamin.kaplan@case.edu> wrote:
> On Sat, Apr 2, 2011 at 11:50 PM, Gnarlodious <gnarlodious@gmail.com> wrote:
>> I get it, you instantiate an object, call a method and get a tuple in
>> response. However, here is what I see:
>>
>>>>> process.communicate()
>> (b'~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied\n',
>> b'')
>>
>> So all I get is the string and no error message, which is the same
>> thing I get with the simpler subprocess.call(). I can parse out the
>> error out and handle it if I need to. Is this a failing in the OSX
>> plutil tool?
>
> Were you expecting a Python error? That's not how POSIX shells work. A
> process that fails just gives you a non-zero return code, not an
> exception. You can call process.poll() to get the return code.

However, using subprocess.check_call() or subprocess.check_output()
will cause a non-zero exit code to raise a Python exception,
CalledProcessError.
http://docs.python.org/library/subprocess.html#subprocess.check_call
http://docs.python.org/library/subprocess.html#subprocess.check_output

Cheers,
Chris

[toc] | [prev] | [next] | [standalone]


#2492

FromGnarlodious <gnarlodious@gmail.com>
Date2011-04-02 21:00 -0700
Message-ID<8d22e277-5f5f-473f-9ddf-21acee876f9a@i4g2000pro.googlegroups.com>
In reply to#2489
On Apr 2, 9:29 pm, Chris Rebert wrote:

> if proc.returncode: # non-zero exit status, indicating error
>     print("Encountered error:")
>     print(error_output) # output the error message
>

Like in my previous post, this only outputs an empty string.
Apparently plutil doesn't communicate well.

-- Gnarlie

[toc] | [prev] | [next] | [standalone]


#2497

FromChris Rebert <clp2@rebertia.com>
Date2011-04-02 21:14 -0700
Message-ID<mailman.153.1301804067.2990.python-list@python.org>
In reply to#2492
<quote what="earlier relevant post" reason="context">
On Sat, Apr 2, 2011 at 8:50 PM, Gnarlodious <gnarlodious@gmail.com> wrote:
> I get it, you instantiate an object, call a method and get a tuple in
> response. However, here is what I see:
>
>>>> process.communicate()
> (b'~/Library/Preferences/iCab/iCab 4 Bookmarks: Permission denied\n',
> b'')
>
> So all I get is the string and no error message,
</quote>

On Sat, Apr 2, 2011 at 9:00 PM, Gnarlodious <gnarlodious@gmail.com> wrote:
> On Apr 2, 9:29 pm, Chris Rebert wrote:
>
>> if proc.returncode: # non-zero exit status, indicating error
>>     print("Encountered error:")
>>     print(error_output) # output the error message
>>
>
> Like in my previous post, this only outputs an empty string.
> Apparently plutil doesn't communicate well.

Yes, it seems to output its error message to stdout, rather than
stderr in line with *nix conventions as one would expect.

Cheers,
Chris
--
http://blog.rebertia.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web