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: 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 Subject: Re: Linux : create a user if not exists Date: Tue, 16 Aug 2011 11:52:13 -0400 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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 "", line 1, in ? > File "", 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