How to Retrieve name of current Windows User (AD or local) using Python?

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.


Try this:

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

That should do the job.


as in https://stackoverflow.com/a/842096/611007 by Konstantin Tenzin

Look at getpass module

import getpass
getpass.getuser()

Availability: Unix, Windows

Note "this function looks at the values of various environment variables to determine the user name. Therefore, this function should not be relied on for access control purposes (or possibly any other purpose, since it allows any user to impersonate any other)."

at least, definitely preferable over getenv .


win32api.GetUserName()

win32api.GetUserNameEx(...) 

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

链接地址: http://www.djcxy.com/p/79432.html

上一篇: 如何确定Python脚本运行的用户和组?

下一篇: 如何使用Python检索当前Windows用户的名称(AD或本地)?