Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11525

Re: Linux : create a user if not exists

References <CA+zNewSOE1R4NRPQfg8edyDZ=ukwsCQnFkaTT9Ssu+5R5u5y9g@mail.gmail.com>
Date 2011-08-16 01:04 -0700
Subject Re: Linux : create a user if not exists
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.46.1313481859.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Aug 16, 2011 at 12:45 AM, smain kahlouch <smainklh@gmail.com> wrote:
> 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
<snip>
> But i think it's a dirty way to do so. Is there another way to manage users
> with python ?

You can replace the /etc/passwd parsing with a call to pwd.getpwnam():
http://docs.python.org/library/pwd.html#pwd.getpwnam

Cheers,
Chris

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Linux : create a user if not exists Chris Rebert <clp2@rebertia.com> - 2011-08-16 01:04 -0700

csiph-web