Linux : create a user if not exists

smain kahlouch smainklh at gmail.com
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
True
>>> 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 cheimes.de>

> Am 16.08.2011 10:04, schrieb Chris Rebert:
> > You can replace the /etc/passwd parsing with a call to pwd.getpwnam():
> > http://docs.python.org/library/pwd.html#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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110816/64d191b4/attachment.html>


More information about the Python-list mailing list