Skip to content

Instantly share code, notes, and snippets.

@TotalLag
Created July 15, 2020 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TotalLag/f85eaf831c29b3a411a69187d9eef317 to your computer and use it in GitHub Desktop.
Save TotalLag/f85eaf831c29b3a411a69187d9eef317 to your computer and use it in GitHub Desktop.
Re-run the program with admin rights in Python
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