如何在不先尝试然后失败的情况下检查是否需要提升权限来启动子进程?
我需要从 python 代码中启动一个子进程命令。在某些情况下,它工作正常,在其他情况下,事实证明需要更高的权限;在某些 Windows 系统上,这种情况会导致我的程序卡住。
我想确定是否需要提升权限,而不是先尝试运行子进程并捕获/捕获任何错误情况。
导致程序卡住的进程被调用;
subprocess.call('路径_文件名_选项',shell=False)
最佳答案
With pywin32, something like the following should work...:
import pythoncom import pywintypes import win32api from win32com.shell import shell if shell.IsUserAnAdmin(): ...
And yes, it seems pywin32 does support Python 3.
由 Alex Martelli 撰写
这也适用于没有 pywin32 的人。
import ctypes
print ctypes.windll.shell32.IsUserAnAdmin()
关于python - 如何确定是否需要提升权限才能在不进行故障检查的情况下启动进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8078708/