Python – check admin rights inside python script

pythonsudo

How to check that my python script is running under Administrator rights (sudo) under BSD-like OS? Need to display user-friendly warning in order it is executed without admin rights.

Best Answer

How about this? Check if uid == 0:

[kbrandt@kbrandt-admin: ~] python -c 'import os; print os.getuid()'
196677

[kbrandt@kbrandt-admin: ~] sudo python -c 'import os; print os.getuid()'
0