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


Groups > comp.lang.python > #11570

Re: Linux : create a user if not exists

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:verizon.net': 0.07; 'terry': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subject:create': 0.09; 'am,': 0.12; 'def': 0.15; 'reedy': 0.16; 'subject:exists': 0.16; 'subject:user': 0.16; 'try/except': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'exists': 0.18; 'jan': 0.19; '(most': 0.21; 'subject:not': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'exist,': 0.23; 'traceback': 0.24; 'subject: : ': 0.25; 'replaced': 0.29; 'print': 0.29; 'to:addr:python-list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.34; 'last):': 0.34; 'see,': 0.34; 'try:': 0.34; 'header:X-Complaints-To:1': 0.35; 'file': 0.36; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'help': 0.39; 'header:Mime-Version:1': 0.39; 'user': 0.39; 'to:addr:python.org': 0.39; 'you.': 0.62; 'flow': 0.68; '"user': 0.84; '10:57': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Linux : create a user if not exists
Date Tue, 16 Aug 2011 11:52:13 -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>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-74-109-121-73.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0
In-Reply-To <CA+zNewQZzmTcD_9T+NA8HKtKQrvo8bFRPwu=A3tcdJ0W7HySDQ@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.75.1313510107.27778.python-list@python.org> (permalink)
Lines 33
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313510107 news.xs4all.nl 23853 [2001:888:2000:d::a6]:41567
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11570

Show key headers only | View raw


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


Thread

Re: Linux : create a user if not exists Terry Reedy <tjreedy@udel.edu> - 2011-08-16 11:52 -0400

csiph-web