Running Python script as root (with sudo) - what is the username of the effective user?

后端 未结 3 439
再見小時候
再見小時候 2020-12-29 09:00

I\'ve recently began using ConfigParser() for my python scripts to add some functionality to them for config files. I know how to use it but I have a problem. My script need

3条回答
  •  春和景丽
    2020-12-29 09:44

    This doesn't work.

    homepath = os.path.expanduser("~/")
    

    So don't use it.

    You want this.

    username= os.environ["LOGNAME"]
    homepath = os.path.expanduser("~"+username+"/")
    

    http://docs.python.org/library/os.html#os.getlogin

    Or perhaps this.

     username= pwd.getpwuid(os.getuid())[0]
     homepath = os.path.expanduser("~"+username+"/")
    

提交回复
热议问题