如何使用admin在Python中运行cmd命令

时间:2014-08-28 20:59:53

标签: python windows-7

我想用Python 3运行一个Windows命令。就像这个os.system(“echo hi”)。但是,如何运行需要管理员访问权限的命令?你怎么做到这一点?感谢。

1 个答案:

答案 0 :(得分:4)

您可以使用Pywin32扩展中包含的ShellExecuteEx Win32 API包装器执行此操作。如果你使用像ActivePython这样的东西,你可能已经有了扩展名。

使用ShellExecuteEx:

import win32com.shell.shell as shell
commands = 'echo hi'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)