The Python pwd module provides access to getpwnam(3) POSIX API, which can be used to get the home directory for a particular user by username, as well determining if the username is valid at all. pwd.getpwnam will raise an exception if called with a non-existent username.
At first it seems like the same result can be achieved in a cross-platform manner via os.path.expanduser('~username'). However, it appears that with Python 2.6 on Windows XP this won’t actually produce a failure for a non-existent username. Furthermore, on Python 2.5 on Windows XP, it seems to fail even for valid users.
Can this information be obtained reliably on Windows? How?
Reading the 2.6 documentation shows that
os.path.expanduser()is broken on Windows:Say whaat? This assumes all user homes have to be under the same parent directory. Nuh-ugh!
It was a bit hard to dig but here is a solution that will look up a local user by given name: