Newbie: problem comparing values

jedi jedi at group6.net
Sun Oct 7 16:05:08 EDT 2001


Hey Jeremy, 

try the following:

import os
user = os.popen("whoami").read()
user = user[:-1]
if user == "root":
  print "You are logged in as root"
else:
  print "You are not logged in as root."
  
The os.popen function returns your answer, along with a new line ('/n') at the end.  if you slice off the last 2 characters, this works fine.  

Hope that helps.

On Mon, Oct 08, 2001 at 08:22:17PM -0500, Jeremy wrote:
> 
> Hi,
> 
> I'm trying to set up a script to check to see whether the person running
> the script is root or not.  If they are, I want to make it exit
> immediately.  This is the code that I have that I'm trying to use:
> 
> 
> user = os.popen("whoami").read()
> if user == "root":
>   print "You are logged in as root"
> else:
>   print "You are not logged in as root."
> 
> 
> The problem is that each time, no matter if it's root or not that's
> running the script, it always says "You are not logged in as root." I
> know that this has to be something small that I'm missing, and please
> forgive me for asking such a "simple" question, but I've only been at
> this for a couple of days (with python.  I have a few months of
> experience with bash scripts).  I've checked about every resource I can
> think of and I still can't figure out what I'm doing wrong.
> 
> Thanks in advance for your help,
> Jeremy
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list

-- 
j3di
jedi at group6.net
http://jedi.group6.net/






More information about the Python-list mailing list