import ctypes
import sys
import subprocess
import os
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if not is_admin():
script_path = os.path.abspath(sys.argv[0])
# 使用 subprocess 启动,避免 ShellExecuteW 的问题
subprocess.run(
['powershell', '-Command', f'Start-Process python -ArgumentList "{script_path}" -Verb RunAs'],
shell=True
)
sys.exit(0)
else:
print("管理员权限已获取")
# 主代码
input("按回车键继续...")