Read stdout from shell command, was: Becoming root

Will Ware wware-nospam at world.std.com
Fri Sep 24 12:23:46 EDT 1999


Robert Vollmert (rvollmert at gmx.net) wrote>
> On Fri, Sep 24, 1999 at 04:14:15AM +0000, Will Ware wrote:
> > if os.popen('whoami').readline()[:-1] == 'root':
> > Dumb question for those wiser than myself:
> > The os.popen().readline()[:-1] construction is pretty ugly.
> > Is there a better way to do it?
> How about 'if os.getuid() == 0:' ?

Thanks, a few others have mentioned this and slight variations.
The thing I'd really like to know is how to do the Python
equivalent of a shell command of this form:

variable = `some shell command`

where "some shell command" prints to standard output.
I find I do this quite frequently, usually using something like
os.popen('some shell command').readline()[:-1] and sure, it works,
but it's really ugly, and I'm always left with the nagging suspicion
that since I didn't explicitly close the os.popen(), it might still
be floating around.
-- 
 - - - - - - - - - - - - - - - - - - - - - - - -
Resistance is futile. Capacitance is efficacious.
Will Ware	email:    wware @ world.std.com




More information about the Python-list mailing list