I need a way to be able to check/test if admin privileges are need to access a folder in python. The one difference that is making me make whole new question about this is that the method should work even if the program has admin privileges.
In theory a function like this should be able to check if admin is required.
import os
def check_admin(path):
"""Returns `True` if admin privileges are required to access `path` and `False` if otherwise"""
try:
os.mkdir(os.path.join(path, "dkfjsdlfjsdkl"))
except PermissionError:
return True
else:
os.rmdir(os.path.join(path, "dkfjsdlfjsdkl"))
return False
And sure enough, doing check_admin(r'C:\Windows')
will return True
. So why am I asking this? Well, if you take that same program and run it as an admin, then check_admin(r'C:\Windows')
returns False
which is not what I want.
I have looked all over SO and none of the answers match the requirenments. Even os.access
doesn't really do what I want (os.access(r"C:\Windows", os.X_OK)
returns True
).
So again, I need a way to be able to check/test if admin privileges are need to access a folder in python.