authentication in python script to run as root

后端 未结 2 1849
故里飘歌
故里飘歌 2020-12-03 07:58

I am doing a project in Linux at system level in Python. So that, I want to know that if i am running my code as a normal user and if i am accessing system files then it sho

2条回答
  •  无人及你
    2020-12-03 08:28

    import os
    euid = os.geteuid() 
    if euid != 0:
      raise EnvironmentError, "need to be root"
      exit()
    

    This won't prompt in the middle of script but would rather force the user to re-run it with sudo or as root

提交回复
热议问题