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