Is there any cross-platform way to check that my Python script is executed with admin rights? Unfortunately, os.getuid()
is UNIX-only and is not available under Windows.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Administrator group membership (Domain/Local/Enterprise) is one thing..
tailoring your application to not use blanket privilege and setting fine grained rights is a better option especially if the app is being used iinteractively.
testing for particular named privileges (se_shutdown se_restore etc), file rights is abetter bet and easier to diagnose.
Try doing whatever you need admin rights for, and check for failure.
This will only work for some things though, what are you trying to do?
It's better if you check which platform your script is running (using
sys.platform
) and do a test based on that, e.g. import some hasAdminRights function from another, platform-specific module.On Windows you could check whether
Windows\System32
is writable usingos.access
, but remember to try to retrieve system's actual "Windows" folder path, probably using pywin32. Don't hardcode one.Here's a utility function I created from the accepted answer: