Get subprocess error output from shell command

Chris Rebert clp2 at rebertia.com
Sat Apr 2 23:29:14 EDT 2011


On Sat, Apr 2, 2011 at 8:07 PM, Gnarlodious <gnarlodious at 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



More information about the Python-list mailing list