我想用Python 3运行Windows命令。就像os.system(“ echo hi”)一样。但是,如何运行需要管理员访问权限的命令?你怎么做到这一点?谢谢。
您可以使用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)
Run Code Online (Sandbox Code Playgroud)