Linux : create a user if not exists

smain kahlouch smainklh at gmail.com
Tue Aug 16 03:45:56 EDT 2011


Hi all,

I'm learning the python language and i'm trying to create a user if it is
not found in the system.
I figured it out by doing the following thing :

>>> def finduser(user):
...     for line in open('/etc/passwd'):
...             if line.startswith(user):
...                     print user, "user exists"
...                     return True
...     return False

>>> if not finduser('myuser'):
...     print "Creating user myuser"
...     os.system('/usr/sbin/useradd myuser')

But i think it's a dirty way to do so. Is there another way to manage users
with python ?

Thank you :)
Sam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110816/2dde1d11/attachment.html>


More information about the Python-list mailing list