作为Windows 10管理员,如何以Python管理员身份运行命令?

时间:2020-10-16 16:28:18

标签: python windows windows-10 administrator elevated-privileges

通过管理员帐户在Windows 7中运行此Python代码非常有效:

import os
os.system('netsh interface set interface "Ethernet" enable')

在Windows 10 中通过管理员帐户运行相同的代码会出现此错误:

请求的操作需要提升(以管理员身份运行)

如何在Windows 10上使其工作?

1 个答案:

答案 0 :(得分:2)

您可以使用win32com.shell.shell模块:

import win32com.shell.shell as shell
commands = 'interface set interface "Ethernet" enable'
shell.ShellExecuteEx(lpVerb='runas', lpFile='netsh.exe', lpParameters=commands)

如果您在导入此模块时遇到问题,请参见here