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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python': 0.08; '>>>>': 0.09; 'parsing': 0.09; 'subject:create': 0.09; 'am,': 0.12; 'def': 0.15; '16,': 0.15; 'subject:exists': 0.16; 'subject:user': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.16; 'language': 0.17; 'cheers,': 0.18; 'cc:no real name:2**0': 0.20; 'subject:not': 0.21; 'trying': 0.21; 'so.': 0.22; 'cc:2**0': 0.22; 'header:In- Reply-To:1': 0.22; 'tue,': 0.23; 'received:209.85.213.46': 0.23; 'received:mail-yw0-f46.google.com': 0.23; 'aug': 0.24; 'subject: : ': 0.25; "i'm": 0.27; 'all,': 0.28; 'message-id:@mail.gmail.com': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.30; 'url:library': 0.31; 'chris': 0.32; 'there': 0.33; 'url:python': 0.36; 'doing': 0.36; 'another': 0.37; 'but': 0.37; 'think': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'skip:o 20': 0.38; 'subject:: ': 0.39; 'url:docs': 0.39; 'user': 0.39; "it's": 0.40; 'skip:. 40': 0.67; '8bit%:93': 0.67; '"user': 0.84; '12:45': 0.84; 'sender:addr:chris': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=Yoj+Fbbqj9bsycgk0czgOAzWZJF0syYXDzJH0jRnmHk=; b=aeLiHj0Y37djOoosjY0zLCdFSVJmIwmYgxOkQrhorBL4Bn6Oz1rXCROa+8TMo64euy zMPTKynVYx2RJ5GoG04d2fDpvv/A6jgiDC4tawlMpBKd88iUxeWOSB1ZYugTrvWk1zk/ muEiwA/oAEohv4Rm+bkF7OehKeVPXx/bQc83E= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: References: Date: Tue, 16 Aug 2011 01:04:11 -0700 X-Google-Sender-Auth: 5zZvg5AzHrlstbsSLEKYh4ovjd4 Subject: Re: Linux : create a user if not exists From: Chris Rebert To: smain kahlouch Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313481859 news.xs4all.nl 23875 [2001:888:2000:d::a6]:45909 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11525 On Tue, Aug 16, 2011 at 12:45 AM, smain kahlouch wrote= : > Hi all, > > I'm learning the python language and i'm trying to create a user if it is > not found in the system. > I figured it out by doing the following thing : > >>>> def finduser(user): > ...=C2=A0=C2=A0=C2=A0=C2=A0 for line in open('/etc/passwd'): > ...=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 if line.startswith(user): > ...=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 print user, "user exist= s" > ...=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return True > ...=C2=A0=C2=A0=C2=A0=C2=A0 return False > But i think it's a dirty way to do so. Is there another way to manage use= rs > with python ? You can replace the /etc/passwd parsing with a call to pwd.getpwnam(): http://docs.python.org/library/pwd.html#pwd.getpwnam Cheers, Chris