Answers for "python run file as admin"

4

how to run python script as admin

import ctypes, sys
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False
if is_admin():
    # Code of your program here
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
Posted by: Guest on November-12-2020
0

run file as administrator python

# Run file as administrator on Windows 7, 8, 10
# Tested for Python 3.8.5
import os

def RunAsAdmin(path_to_file,*args):
	os.system(r'Powershell -Command "Start-Process "'+path_to_file+'"'+ # CMD running Powershell
				' -ArgumentList @('+str(args)[1:-1]+')'+ # Arguments. [1:-1] to remove brackets
				' -Verb RunAs"' # Run file as administrator
	)

RunAsAdmin('cmd.exe','arg1','arg2')
Posted by: Guest on May-16-2021

Python Answers by Framework

Browse Popular Code Answers by Language