如何使用管理员在 Python 中运行 cmd 命令 [英] How to run cmd command in Python with admin

查看:171
本文介绍了如何使用管理员在 Python 中运行 cmd 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to run a windows command with Python 3. Like this os.system("echo hi"). However, How about running a command that requires admin access? How do you do this? Thanks.

推荐答案

您可以使用 Pywin32 扩展.如果您使用的是 ActivePython 之类的东西,您可能已经有了这些扩展.

You can do this with the ShellExecuteEx Win32 API wrapper included in the Pywin32 extensions. If you are using something like ActivePython you may already have the extensions.

使用 ShellExecuteEx :

To use ShellExecuteEx :

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

这篇关于如何使用管理员在 Python 中运行 cmd 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆