Created
July 15, 2020 02:29
-
-
Save TotalLag/f85eaf831c29b3a411a69187d9eef317 to your computer and use it in GitHub Desktop.
Re-run the program with admin rights in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import ctypes | |
def is_admin(): | |
try: | |
return ctypes.windll.shell32.IsUserAnAdmin() | |
except: | |
return False | |
if is_admin(): | |
# Do stuff | |
else: | |
# Re-run the program with admin rights | |
sys.stdout.write('Not running as admin, relaunching...?\n') | |
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment