Skip to Main Content
Python Programming On Win32
book

Python Programming On Win32

by Andy Robinson, Mark Hammond
January 2000
Intermediate to advanced content levelIntermediate to advanced
672 pages
21h 46m
English
O'Reilly Media, Inc.
Content preview from Python Programming On Win32

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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Programming - Second Edition

Advanced Python Programming - Second Edition

Quan Nguyen

Publisher Resources

ISBN: 1565926218Supplemental ContentErrata Page