Change to sudo user within a python script

前端 未结 8 1874
挽巷
挽巷 2020-11-29 05:33

I have a problem. I am writing a piece of software, which is required to perform an operation which requires the user to be in sudo mode. running \'sudo python filename.py\'

8条回答
  •  醉梦人生
    2020-11-29 06:00

    Don't try and make yourself sudo just check if you are and error if your not

    class NotSudo(Exception):
        pass
    
    if os.getuid() != 0:
        raise NotSudo("This program is not run as sudo or elevated this it will not work")
    

提交回复
热议问题