Dup Ver Goto 📝

force-root

PT2/lang/python/snippets does not exist
To
16 lines, 77 words, 570 chars Page 'force-root' does not exist.

Put this into a file named force_root.py

import sys, os
print("Forcing root")
if os.getenv("USER") != "root":
  os.execv("/usr/bin/sudo",["sudo"]+sys.argv)

and put that in your default python path (it needs to be in roots PYTHONPATH, which you can inspect by running python -c 'import sys; print(sys.path)' — for example on the Kubuntu system I'm writing this on, I put it in /usr/local/lib/python3.10/dist-packages.

Then

import force_root

#rest of script

and this will automatically run sudo for you.