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


Groups > comp.lang.python > #11525 > unrolled thread

Re: Linux : create a user if not exists

Started byChris Rebert <clp2@rebertia.com>
First post2011-08-16 01:04 -0700
Last post2011-08-16 01:04 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#11525 — Re: Linux : create a user if not exists

FromChris Rebert <clp2@rebertia.com>
Date2011-08-16 01:04 -0700
SubjectRe: Linux : create a user if not exists
Message-ID<mailman.46.1313481859.27778.python-list@python.org>
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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web