Request UAC elevation from within a Python script?

前端 未结 11 778

I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 05:33

    It took me a little while to get dguaraglia's answer working, so in the interest of saving others time, here's what I did to implement this idea:

    import os
    import sys
    import win32com.shell.shell as shell
    ASADMIN = 'asadmin'
    
    if sys.argv[-1] != ASADMIN:
        script = os.path.abspath(sys.argv[0])
        params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
        shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
        sys.exit(0)
    

提交回复
热议问题