Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news-transit.tcx.org.uk!rt.uk.eu.org!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:p 40': 0.04; 'importerror:': 0.07; 'parsing': 0.09; 'readable': 0.09; 'url:activestate': 0.09; 'username,': 0.09; 'win32': 0.12; 'def': 0.13; 'thanks!': 0.14; '"1"': 0.16; '(),': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'hmm.': 0.16; 'key/value': 0.16; 'message-id:@tim.thechases.com': 0.16; 'posix': 0.16; 'pwd': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'skip:) 10': 0.16; 'subject:user': 0.16; 'win32api': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'subject:information': 0.21; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'module,': 0.23; 'cc:2**0': 0.24; 'code': 0.25; 'suggestion': 0.26; 'code.': 0.26; 'module': 0.26; 'windows': 0.26; 'import': 0.27; 'url:code': 0.28; 'pass': 0.29; 'yield': 0.29; 'cc:addr:python.org': 0.29; 'thanks': 0.31; "i've": 0.31; 'skip:( 20': 0.31; 'version': 0.32; 'comment': 0.32; "isn't": 0.33; 'header:User-Agent:1': 0.33; 'there': 0.33; 'probably': 0.34; 'try:': 0.34; 'changing': 0.35; 'something': 0.35; 'but': 0.37; 'except': 0.37; 'e.g.': 0.39; 'returned': 0.39; "i'd": 0.39; 'more': 0.61; 'full': 0.62; 'cameron': 0.73; 'hand,': 0.76; 'username': 0.77; '19:44,': 0.84; '_if_': 0.84; 'often.': 0.84; 'win32:': 0.84 Date: Sat, 10 Dec 2011 07:31:41 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Cameron Simpson Subject: Re: Obtaining user information References: <4EE2B990.2030908@tim.thechases.com> <20111210073723.GA939@cskk.homeip.net> In-Reply-To: <20111210073723.GA939@cskk.homeip.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: Python 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323523906 news.xs4all.nl 6849 [2001:888:2000:d::a6]:49548 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16951 On 12/10/11 01:37, Cameron Simpson wrote: > On 09Dec2011 19:44, Tim Chase wrote: > | Currently I can get the currently-logged-in-userid via > | getpass.getuser() which would yield something like "tchase". > > _If_ you're on a terminal. _And_ that's exactly what you want. > Personally I need to the name of geteuid() or getuid() more often. yes, it deals with emailing so the local userid and full-name are what I want. > | Is there a cross-platform way to get the full username (such as from > | the GECOS field of /etc/passed or via something like NetUserGetInfo > | on Win32 so I'd get "Tim Chase" instead? > > Hmm. Doesn't windows have a posix layer? > > pwd.getpwnam(os.getuid())[4].split(',')[0] > > is the best I've got. ANd it probably doesn't work in Windows:-( well, that's a more readable version of my hand-crafted opening of /etc/passwd and parsing by hand, so thanks! As you mention, the pwd module isn't available on Win32 so I still have to branch my code. I found Tim Golden's suggestion in a comment on ActiveState[1] that gave this one-liner for Win32: win32net.NetUserGetInfo (win32net.NetGetAnyDCName (), win32api.GetUserName (), 1) By changing the "1" to a "20", one of the returned key/value pairs was "full_name" and the username, so my code currently reads: def get_user_info(): "Return (userid, username) e.g. ('jsmith', 'John Smith')" userid = username = getpass.getuser() if sys.platform.lower().startswith("win"): try: import win32net, win32api USER_INFO_2 = 2 username = win32net.NetUserGetInfo( win32net.NetGetAnyDCName(), win32api.GetUserName(), USER_INFO_2, )["full_name"] or username except ImportError: pass # no win32* module, so default to userid else: import pwd username = pwd.getpwnam(userid).pw_gecos.split(',',1)[0] return userid, username It only addresses Win32 and Posix, but that's what I need for now. Thanks again. -tkc [1] http://code.activestate.com/recipes/66314-get-user-info-on-windows-for-current-user/