How to use shell return value like $? In python?

Chris Rebert clp2 at rebertia.com
Sun Oct 23 22:58:37 EDT 2011


On Sun, Oct 23, 2011 at 7:51 PM,  <aaabbb16 at hotmail.com> wrote:
> On Oct 23, 7:44 pm, aaabb... at hotmail.com wrote:
>> exp:
>> os.system('ls -al')
>> #I like to catch return value after this command. 0 or 1,2,3....
>> does python support to get "$?"?
>> then I can use something like:
>>  If $?==0:
<snip>
> So for what I do is:
> r_number =os.system('ls -al')
>     if r_number == 0
>      .........
>      .........
> any other way?

I would recommend using the `subprocess` module instead:
http://docs.python.org/library/subprocess.html#convenience-functions

Cheers,
Chris



More information about the Python-list mailing list