1import ctypes, sys
2def is_admin():
3 try:
4 return ctypes.windll.shell32.IsUserAnAdmin()
5 except:
6 return False
7if is_admin():
8 # Code of your program here
9else:
10 # Re-run the program with admin rights
11 ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
12
13
1import ctypes, os
2
3def isAdmin():
4 """ Return True/Flase """
5 try:
6 is_admin = (os.getuid() == 0) # if Unis
7 except AttributeError:
8 is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 # elese if Windows
9 return is_admin