Get subprocess error output from shell command

Benjamin Kaplan benjamin.kaplan at case.edu
Sat Apr 2 23:25:58 EDT 2011


On Sat, Apr 2, 2011 at 11: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.
>
> -- 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()



More information about the Python-list mailing list