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


Groups > comp.lang.python > #16951 > unrolled thread

Re: Obtaining user information

Started byTim Chase <python.list@tim.thechases.com>
First post2011-12-10 07:31 -0600
Last post2011-12-10 07:31 -0600
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Obtaining user information Tim Chase <python.list@tim.thechases.com> - 2011-12-10 07:31 -0600

#16951 — Re: Obtaining user information

FromTim Chase <python.list@tim.thechases.com>
Date2011-12-10 07:31 -0600
SubjectRe: Obtaining user information
Message-ID<mailman.3493.1323523906.27778.python-list@python.org>
On 12/10/11 01:37, Cameron Simpson wrote:
> On 09Dec2011 19:44, Tim Chase<python.list@tim.thechases.com>  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/





[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web