bash command, get stdErr

Tracubik affdfsdfdsfsd at b.com
Thu Aug 25 04:25:59 EDT 2011


Hi all!
i'ld like to execute via Python this simple bash command:

sudo las

las is intended to be a typo for "ls"

the point is that i want to see in the terminal the stderr message (that 
is "sorry, try again" if i insert the wrong password or "sudo: las: 
command not found" otherwise)

i can simply do it with subprocess.POpen() or subprocess.Call() but as 
far as i know i have two choice: 
1) s = subprocess.Popen('sudo las', shell=True, stderr=subprocess.PIPE)

in this way i catch the stderr messages BUT they are not "printed" in the 
terminal

2) s = subprocess.Popen('sudo las', shell=True, stderr=none)
in this way i "print" the message in the terminal but i can retrieve they 
(the error messages) when the command is executed

it's important for me to know if the user have inserted the wrong 
password or i've tryied to execute a wrong/unsupported command (for 
example yum install in a ubuntu environment, or a typo like "las")

if i use stderr=subprocess.PIPE and the user insert a wrong password, 
he'll get a second request of insert password without the reason for 
doing it (because the error message isn't printed on the terminal). This 
is not a big problem, he will understand he haven't inserted the right 
password, and if he miss the password 3 times i'll pop-up a gtk alert 
windows to inform him of the problem. Still, it's not good to don't give 
feedback of the error to the user, so i'ld like to print the stderr on 
terminal AND get it after the command terminate to check the problem 
occurred (exit status is not enough).

Is there a way to accomplish this?
As usual sorry for my bad english
MedeoTL 



More information about the Python-list mailing list