Obtaining user information
Tim Chase
python.list at tim.thechases.com
Sat Dec 10 08:31:41 EST 2011
More information about the Python-list mailing list
Sat Dec 10 08:31:41 EST 2011
- Previous message (by thread): Obtaining user information
- Next message (by thread): Obtaining user information
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 12/10/11 01:37, Cameron Simpson wrote: > On 09Dec2011 19:44, Tim Chase<python.list at 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/
- Previous message (by thread): Obtaining user information
- Next message (by thread): Obtaining user information
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list