私信  •  关注

Michael Kropat

Michael Kropat 最近创建的主题
Michael Kropat 最近回复了

通过Windows API可以在Windows上获得当前用户名,不过通过ctypes FFI调用有点麻烦( GetCurrentProcess → OpenProcessToken → GetTokenInformation → LookupAccountSid ).

我写了一个小模块,可以直接从Python中实现这一点, getuser.py . 用法:

import getuser
print(getuser.lookup_username())

它可以在Windows和*nix上运行(后者使用 pwd 其他答案中描述的模块)。