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


Groups > comp.lang.python > #11571

Re: Linux : create a user if not exists

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <alex.kapps@web.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.028
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'else:': 0.03; 'subject:create': 0.09; 'def': 0.15; 'message-id:@web.de': 0.16; 'subject:exists': 0.16; 'subject:user': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'exists': 0.18; '(most': 0.21; 'subject:not': 0.21; "doesn't": 0.22; 'header:In-Reply-To:1': 0.22; 'traceback': 0.24; 'subject: : ': 0.25; 'replaced': 0.29; 'print': 0.29; 'from:addr:web.de': 0.30; 'to:addr:python-list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.34; 'last):': 0.34; 'try:': 0.34; 'file': 0.36; 'none': 0.37; 'but': 0.37; 'subject:: ': 0.39; 'help': 0.39; 'user': 0.39; 'needed.': 0.39; 'to:addr:python.org': 0.39; 'you.': 0.62; 'details': 0.65; 'received:172.20': 0.73; '"user': 0.84; 'sender:addr:web.de': 0.84
Date Tue, 16 Aug 2011 17:56:37 +0200
From Alexander Kapps <alex.kapps@web.de>
User-Agent Mozilla/5.0 (X11; U; Linux i686; de-DE; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11
MIME-Version 1.0
To python-list@python.org
Subject Re: Linux : create a user if not exists
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>
In-Reply-To <CA+zNewQZzmTcD_9T+NA8HKtKQrvo8bFRPwu=A3tcdJ0W7HySDQ@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
Sender alex.kapps@web.de
X-Sender Alex.Kapps@web.de
X-Provags-ID V01U2FsdGVkX19EEd+tX6t5A2YLFY4oCjdx5nZ9ld8lCpItmIbW x6VCkjMDjCg5gqJtjowssAm5ury6RrMg9fxsQygc539aM4ujPb G4lyk9MhA=
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.76.1313510199.27778.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313510199 news.xs4all.nl 23908 [2001:888:2000:d::a6]:59165
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11571

Show key headers only | View raw


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 "<stdin>", line 1, in ?
>    File "<stdin>", 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)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Linux : create a user if not exists Alexander Kapps <alex.kapps@web.de> - 2011-08-16 17:56 +0200

csiph-web