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

前端 未结 12 818
别那么骄傲
别那么骄傲 2020-11-22 03:11

Is there a portable way to get the current user\'s username in Python (i.e., one that works under both Linux and Windows, at least). It would work like os.getuid

12条回答
  •  别那么骄傲
    2020-11-22 03:56

    Look at getpass module

    import getpass
    getpass.getuser()
    'kostya'
    

    Availability: Unix, Windows


    p.s. Per comment below "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)."

提交回复
热议问题