Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.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.021 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'else:': 0.03; 'subject:create': 0.09; 'def': 0.15; 'subject:exists': 0.16; 'subject:user': 0.16; '\xe9crit': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'exists': 0.18; 'received:74.125.82.174': 0.19; 'received :mail-wy0-f174.google.com': 0.19; '(most': 0.21; 'subject:not': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'traceback': 0.24; 'subject: : ': 0.25; 'all,': 0.28; 'replaced': 0.29; 'print': 0.29; 'to:addr:python-list': 0.33; '...': 0.34; 'header :User-Agent:1': 0.34; 'message-id:@gmail.com': 0.34; 'last):': 0.34; 'try:': 0.34; 'thank': 0.35; 'file': 0.36; 'none': 0.37; 'but': 0.37; 'received:74.125.82': 0.38; 'received:google.com': 0.38; 'subject:: ': 0.39; 'received:192': 0.39; 'help': 0.39; 'user': 0.39; 'needed.': 0.39; 'to:addr:python.org': 0.39; 'received:74.125': 0.39; 'you.': 0.62; 'details': 0.65; '"user': 0.84; 'sam': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=ESRQ9NVy8dZPyDk4XHUdDE/t3qdo+tik7f4s6HTh0mw=; b=lUHeOqzJKk6oCJYqmei7GHO2WfWHlUK5mhGP+BuQHw748QTqL0dia1lQ0riYqE+MYI X3COHkD3vqHBOze6zuRwt0FwtPBLU6Ry1XpoOFz8pnpuXAGOIPcDoox/eoU2RPoAq3BR rA8SduRDzXv+xXUFmZkgea0KYg/2c31/ZF1+Y= Date: Tue, 16 Aug 2011 19:47:27 +0200 From: Smaine Kahlouch User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Linux : create a user if not exists References: <4E4A9335.4010700@web.de> In-Reply-To: <4E4A9335.4010700@web.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313516840 news.xs4all.nl 23882 [2001:888:2000:d::a6]:41437 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11591 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 "", line 1, in ? >> File "", 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