Run process as admin with subprocess.run in python(在 python 中使用 subprocess.run 以管理员身份运行进程)
问题描述
有没有办法将一些 runas=True
arg 传递给 python 中的 subprocess.run
函数?我想以管理员身份运行一个进程(提升它).感谢您的回答:)
Is there a way of passing some runas=True
arg to a subprocess.run
function in python? I want to run a process as admin (elevate it). Thanks for answers :)
使用 Windows 操作系统.
Using Windows OS.
推荐答案
由于没有指定OS,我以MS Windows OS为例
since OS is not specified, I will take the example of MS Windows OS
Windows 有一个命令行实用程序Run as",可以用作
Windows has a command line utility "Run as", which can be used as
runas [{/profile |/noprofile}] [/env] [{/netonly |/savecred}] [/smartcard] [/showtrustlevels] [/trustlevel]/user:<UserAccountName><程序名><PathToProgramFile>"
供进一步参考https://technet.microsoft.com/en-us/library/cc771525.aspx
您可以在下面的代码中使用它
You can use this in code like below
import subprocess as sp
prog = sp.Popen(['runas', '/noprofile', '/user:Administrator', 'NeedsAdminPrivilege.exe'],stdin=sp.PIPE)
prog.stdin.write('password')
prog.communicate()
这篇关于在 python 中使用 subprocess.run 以管理员身份运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 python 中使用 subprocess.run 以管理员身份运行进程


基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01