O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Obtaining Information About a User or Group

To get a feel for this, let’s start by querying information about a current user. First, obtain your username:

>>> import win32api
>>> userName=win32api.GetUserName()

And to assist working with the Python dictionaries, you can define a simple helper function to pretty-print the data:

>>> def dump(dict):
...     for key, value in dict.items():
...         print key, "=", str(value)
...     
>>>

So now you can get the user information and pass it to your function to print. Pass None for the first parameter, so this function obtains the information from the local machine. Pass your current username in the second parameter, and request information level 1 in the last parameter, giving the data defined in PyUSER_INFO_1:

>>> import win32net
>>> info=win32net.NetUserGetInfo(None, userName, 1)
>>> print info['name'] # print just the user name
skip
>>> dump(info)
priv = 2
home_dir = c:\winnt\profiles\skip\personal
password = None
script_path = 
name = skip
flags = 66049
password_age = 23792806
comment = 
>>>

By referring to Appendix B, you can determine the information returned for each information level; however, for a thorough description, you should refer to the Win32 documentation for these functions. Let’s experiment with this a little from the interactive prompt:

>>> len(info)
8

Level 1 (PyUSER_INFO_1) has eight items of data. You can try some other levels:

>>> info=win32net.NetUserGetInfo(None, userName, 2) >>> len(info) 24 >>> info=win32net.NetUserGetInfo(None, userName, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required