How to run cmd command in Python with admin

前端 未结 1 360
误落风尘
误落风尘 2021-01-18 05:46

I want to run a windows command with Python 3. Like this os.system(\"echo hi\"). However, How about running a command that requires admin access? How do you do this? Thanks.

相关标签:
1条回答
  • 2021-01-18 06:25

    You can do this with the ShellExecuteEx Win32 API wrapper included in the Pywin32 extensions. If you are using something like ActivePython you may already have the extensions.

    To use ShellExecuteEx :

    import win32com.shell.shell as shell
    commands = 'echo hi'
    shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)
    
    0 讨论(0)
提交回复
热议问题