how to get linux username in a python program

This question already has an answer here:

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

  • Could os.getlogin() be what you are looking for? (https://docs.python.org/3/library/os.html#os.getlogin)

    >>> import os
    >>> os.getlogin()
    'msurrow'
    >>> 
    

    Or maybe getpass.getuser() (https://docs.python.org/3.1/library/getpass.html)

    >>> import getpass
    >>> getpass.getuser()
    'msurrow'
    >>> 
    

    There is a difference between who the logged in user is, and 'who' the python process is running as. Jeff explains more in his answer here

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

    上一篇: 从Python脚本中获取用户

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