views:

1580

answers:

3

How can I retrieve the name of the currently logged in user, using a python script? The function should work regardless of whether it is a domain/ad user or a local user.

+5  A: 

Try this:

import os;
print os.environ.get( "USERNAME" )

That should do the job.

Steve Kemp
Is it get() or getenv()?
Marcel Levy
http://www.a-a-p.org/exec/tutor-python.html seems to support my usage.
Steve Kemp
A: 

I don't know Python, but for windows the underlying api is GetUserNameEx, I assume you can call that in Python if os.environ.get( "USERNAME" ) doesn't tell you everything you need to know.

Jim In Texas
+1  A: 

win32api.GetUserName()

win32api.GetUserNameEx(...)

See: http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html

Adam