Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #11570
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Linux : create a user if not exists |
| Date | 2011-08-16 11:52 -0400 |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.75.1313510107.27778.python-list@python.org> (permalink) |
On 8/16/2011 10:57 AM, 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'
>
> As you can see, i just want to look for a user and if it doesn't exist,
> an action is taken (create user).
Replace if/then with try/except control flow
try:
pwd.getpwnam(user)
return True
except KeyWordError:
return False
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Linux : create a user if not exists Terry Reedy <tjreedy@udel.edu> - 2011-08-16 11:52 -0400
csiph-web