bash command, get stdErr

Tracubik affdfsdfdsfsd at b.com
Fri Aug 26 10:26:32 CEST 2011


Il Thu, 25 Aug 2011 08:25:59 +0000, Tracubik ha scritto:

> 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

maybe i've solved it:
sudo las 2>&1 | tee tmp.txt

in this way i redirect stderr to stdout (the terminal) and i tee it in 
tmp.txt.
The only problem is the output and error messages are joined togheter but 
i think i can deal with it.

:-D
MedeoTL




More information about the Python-list mailing list