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


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

Re: Linux : create a user if not exists

Started bySmaine Kahlouch <smainklh@gmail.com>
First post2011-08-16 19:47 +0200
Last post2011-08-16 19:47 +0200
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 Smaine Kahlouch <smainklh@gmail.com> - 2011-08-16 19:47 +0200

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

FromSmaine Kahlouch <smainklh@gmail.com>
Date2011-08-16 19:47 +0200
SubjectRe: Linux : create a user if not exists
Message-ID<mailman.90.1313516840.27778.python-list@python.org>
Le 16/08/2011 17:56, Alexander Kapps a écrit :
> On 16.08.2011 16:57, smain kahlouch wrote:
>> 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'
>
>
> Untested:
>
> def finduser(name):
>     try:
>         return pwd.getpwnam(name)
>     except KeyError:
>         return None
>
> if not finduser("myuser"):
>     print "creating user..."
> else:
>     print "user already exists"
>
>
> Has the advantage that finduser() returns the user details if needed.
>
>
> (BTW: Please don't top-post)

Ok Thank you all, that works perfectly :)

See you soon,
Sam

[toc] | [standalone]


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


csiph-web