Python代码集----Python实现以管理员权限启动Windows应用程序(以腾讯会议为例)

【原文链接】Python代码集----Python实现以管理员权限启动Windows应用程序(以腾讯会议为例)

(1)Windows上为了安全起见,python语言启动的应用程序默认都不会使用admin管理员权限,但是在有些情况下,我们又需要使用管理员权限启动应用程序,否则可能涉及一些权限的问题等导致应用程序无法使用

(2)如下代码为python通过管理员权限启动腾讯会议的例子,启动其他应用程序的可参考使用即可,这里只需要将需要使用管理员权限执行代码放入下面的main函数即可。其他代码无需改动,可直接使用。

import os
import sys
import ctypes


def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

def run_cmd(cmd):
    res = os.popen(cmd)
    output=res.buffer.read().decode("gbk")
    return output

def main():
    run_cmd("D:/ProgrameFile/WeMeet/wemeetapp.exe")

if __name__=="__main__":
    if is_admin():
        print("now is already admin")
        main()
    else:
        print("run as admin")
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)

猜你喜欢

转载自blog.csdn.net/redrose2100/article/details/129968272
今日推荐