Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11591
| Date | 2011-08-16 19:47 +0200 |
|---|---|
| From | Smaine Kahlouch <smainklh@gmail.com> |
| Subject | Re: Linux : create a user if not exists |
| References | <CA+zNewSOE1R4NRPQfg8edyDZ=ukwsCQnFkaTT9Ssu+5R5u5y9g@mail.gmail.com> <CAMZYqRRwf8ePhEMAAS2LKWGardXLzfMR7as=2Q1yQ2yK_QBvqQ@mail.gmail.com> <j2du0c$gau$1@dough.gmane.org> <CA+zNewQZzmTcD_9T+NA8HKtKQrvo8bFRPwu=A3tcdJ0W7HySDQ@mail.gmail.com> <4E4A9335.4010700@web.de> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.90.1313516840.27778.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Linux : create a user if not exists Smaine Kahlouch <smainklh@gmail.com> - 2011-08-16 19:47 +0200
csiph-web