Read stdout from shell command, was: Becoming root

Bjorn Pettersen bjorn at roguewave.com
Fri Sep 24 12:37:41 EDT 1999


The commands module works well:

	import commands
	variable = commands.getoutput('some shell command')

-- bjorn

> -----Original Message-----
> From: wware-nospam at world.std.com [mailto:wware-nospam at world.std.com]
> Sent: Friday, September 24, 1999 10:24 AM
> To: python-list at python.org
> Subject: Read stdout from shell command, was: Becoming root
> 
> 
> 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
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list