Linux : create a user if not exists

smain kahlouch smainklh at
Tue Aug 16 16:57:27 CEST 2011

Ok than you. You're right but it doesn't help me :
I replaced it :

>>> def finduser(user):
...     if pwd.getpwnam(user):
...             print user, "user exists"
...             return True
...     return False
>>> finduser('realuser')
realuser user exists
>>> finduser('blabla')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 2, in finduser
KeyError: 'getpwnam(): name not found: blabla'

As you can see, i just want to look for a user and if it doesn't exist, an
action is taken (create user).
Maybe you have another way to do that check ?

Thank you

2011/8/16 Christian Heimes <lists at>

> Am 16.08.2011 10:04, schrieb Chris Rebert:
> > You can replace the /etc/passwd parsing with a call to pwd.getpwnam():
> >
> You should replace it. /etc/passwd is not the only source for users.
> /etc/nsswitch.conf may list more user and group sources like nis/yp,
> LDAP and so on.
> Christian
> --
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list