Change to sudo user within a python script

前端 未结 8 1909
挽巷
挽巷 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 05:41

    It is better to run as little of the program as possible with elevated privileges. You can run the small part that needs more privilege via the subprocess.call() function, e.g.

    import subprocess
    returncode = subprocess.call(["/usr/bin/sudo", "/usr/bin/id"])
    

提交回复
热议问题