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

时间:2022-11-06 02:56:07

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.

如何使用python脚本检索当前登录用户的名称?无论是域/广告用户还是本地用户,该功能都应该有效。

5 个解决方案

#1


27  

Try this:

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

That should do the job.

那应该做的。

#2


4  

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

如Konstantin Tenzin的https://*.com/a/842096/611007

Look at getpass module

看看getpass模块

import getpass
getpass.getuser()

Availability: Unix, Windows

可用性: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.

至少,绝对优于getenv。

#3


2  

win32api.GetUserName()

win32api.GetUserNameEx(...) 

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

#4


1  

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.

我不知道Python,但对于Windows,底层API是GetUserNameEx,我假设你可以在Python中调用它,如果os.environ.get(“USERNAME”)没有告诉你需要知道的一切。

#5


-1  

Pretty old question but to refresh the answer to the original question "How can I retrieve the name of the currently logged in user, using a python script?" use:

相当古老的问题,但刷新原始问题的答案“如何使用python脚本检索当前登录用户的名称?”使用:

import os
print (os.getlogin())

Per Python documentation: getlogin - Return the name of the user logged in on the controlling terminal of the process.

每个Python文档:getlogin - 返回在进程的控制终端上登录的用户的名称。

#1


27  

Try this:

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

That should do the job.

那应该做的。

#2


4  

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

如Konstantin Tenzin的https://*.com/a/842096/611007

Look at getpass module

看看getpass模块

import getpass
getpass.getuser()

Availability: Unix, Windows

可用性: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.

至少,绝对优于getenv。

#3


2  

win32api.GetUserName()

win32api.GetUserNameEx(...) 

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

#4


1  

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.

我不知道Python,但对于Windows,底层API是GetUserNameEx,我假设你可以在Python中调用它,如果os.environ.get(“USERNAME”)没有告诉你需要知道的一切。

#5


-1  

Pretty old question but to refresh the answer to the original question "How can I retrieve the name of the currently logged in user, using a python script?" use:

相当古老的问题,但刷新原始问题的答案“如何使用python脚本检索当前登录用户的名称?”使用:

import os
print (os.getlogin())

Per Python documentation: getlogin - Return the name of the user logged in on the controlling terminal of the process.

每个Python文档:getlogin - 返回在进程的控制终端上登录的用户的名称。