简体   繁体   English

如何在python中以管理员模式打开命令提示符?

[英]How to open command prompt in Administrator mode in python?

I'm a bit of a noob, so sorry in advance if I'm not doing this right.我有点菜鸟,如果我做的不对,请提前道歉。

I'm using python 3.6x我正在使用 python 3.6x

What I have so far that gets the command window open is到目前为止,我打开命令窗口的是

import os
os.open("start cmd")

but this comes up in the directory that I'm working from and not in administration mode.但这出现在我正在工作的目录中,而不是在管理模式下。

I've also tried我也试过

import os
os.system("start /wait cmd /wait {tskill dwm}")

but that didn't work either.但这也没有用。 ( tskill dwm is what I'm trying to get python to write in to command manager to fix a bug with windows' buttons not going away) tskill dwm是我试图让 python 写入命令管理器以修复 Windows 按钮不会消失的错误)

My overall goal is to just click this python script Blah.py and have it restart the windows viewer or whatever it's called.我的总体目标是单击此 python 脚本Blah.py并让它重新启动 Windows 查看器或任何它所谓的。 Doing this clears the stuck buttons.这样做会清除卡住的按钮。 Overall this is just an exercise in practicing python.总的来说,这只是练习python的练习。 I know I could just disable the button fade out and that would take care of the issue.我知道我可以禁用按钮淡出,这样就可以解决问题。 I just figured this would be a good learning opportunity.我只是认为这将是一个很好的学习机会。

from pynput.keyboard import Key, Controller
import time
keyboard = Controller()

keyboard.press(Key.cmd)
keyboard.release(Key.cmd)
time.sleep(0.3)

keyboard.type("cmd")
time.sleep(1)
keyboard.press(Key.right)
keyboard.release(Key.right)
keyboard.press(Key.down)
keyboard.release(Key.down)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
time.sleep(0.5)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.press(Key.tab)
keyboard.release(Key.tab)
keyboard.press(Key.enter)
keyboard.release(Key.enter)

exit

The answer is here答案在这里

https://stackoverflow.com/a/11746382/7352806 https://stackoverflow.com/a/11746382/7352806

import os
import sys
import win32com.shell.shell as shell
ASADMIN = 'asadmin'
if sys.argv[-1] != ASADMIN:
    script = os.path.abspath(sys.argv[0])
    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])
    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)
    sys.exit(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM