Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Is it any cross-platform way to check that my python script is executed under admin rights? Unfortunately, os.getuid() is UNIX-only and is not available under windows :(.

share|improve this question

2 Answers 2

up vote 3 down vote accepted
import ctypes, os
try:
 is_admin = os.getuid() == 0
except:
 is_admin = ctypes.windll.shell32.IsUserAnAdmin()

print is_admin
share|improve this answer

"Admin rights" is meaningless. Both Unix and Windows have a long list of different privileges that a particular user may or may not have. You should just try to do what you want to do and use exceptions to deal with lack of privileges. That's that they're there for.

share|improve this answer
    
I need to test if script was executed under "su" on unix/macos and under "run as administrator" under vista+. –  Eye of Hell Jun 22 '09 at 11:04
    
Using exceptions is good, but i need to use third-party applications that have undefined behavior if not run via su/admin, and i do not want to trash a system just to check that script has sufficient rights :) –  Eye of Hell Jun 22 '09 at 11:05
    
I agree asking for forgiveness rather than permission is right most of the time. But as the OP points out, it's not right all of the time –  Server Horror Jun 22 '11 at 0:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.