Cross platform solution for getting current login name in Python

This question already has an answer here:

  • Is there a portable way to get the current username in Python? 11 answers

  • getpass.getuser()是你的朋友。


    这是我使用的:

    import os
    username = getattr(os, "getlogin", None)
    if not username:
        for var in ['USER', 'USERNAME','LOGNAME']:
            if  var in os.environ:
                username = os.environ[var]
    print("username: %s" % (username))
    
    链接地址: http://www.djcxy.com/p/79420.html

    上一篇: 如何在python程序中获取linux用户名

    下一篇: 用Python获取当前登录名的跨平台解决方案