当前位置:   article > 正文

python 以管理员权限调用cmd命令_python管理员执行cmd命令行

python管理员执行cmd命令行

一,将要执行的cmd命令放到xx.bat文件里

二,创建shell.vbs使系统以管理员权限运行

  1. cwd = CreateObject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.Path
  2. path = cwd & "\RestartInternet.bat"
  3. Set shell = CreateObject("Shell.Application")
  4. shell.ShellExecute path,"","","runas",1
  5. WScript.Quit

三,python调用

  1. # !/usr/bin/python3
  2. # coding: utf-8
  3. import os
  4. import subprocess
  5. import traceback
  6. def runAdmin(cmd, timeout=1800000):
  7. # 这一段是将要执行的cmd命令写入.bat, 如果已经有创建好的.bat, 则这一段可以注释掉
  8. f = None
  9. try:
  10. bat = os.getcwd() + r"\RestartInternet.bat"
  11. f = open(bat, 'w')
  12. f.write(cmd)
  13. except Exception as e:
  14. traceback.print_exc()
  15. raise e
  16. finally:
  17. if f:
  18. f.close()
  19. try:
  20. shell = os.getcwd() + r"\shell.vbs"
  21. sp = subproce
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号