python程序以管理员权限运行


python写的脚本有时候需要管理员权限怎么办?

直接在代码里添加权限运行

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, __file__, None, 1)

posted @ 2023-09-23 15:36  ElaineTiger  阅读(278)  评论(0)    收藏  举报  来源