How to determine if elevated priveleges are required to start a process without checking-by-failure

625 views Asked by At

How do I go about checking whether privelege elevation is required to start a subprocess without trying first then failing?

I need to start a subprocess command from within python code. In some cases it works fine, in other cases it turns out that elevated priveleges are required; and on some windows systems this condition causes my program to freeze.

I would like to determine wether privelege escalation is required without attempting to first run the subprocess and catching / traping any error condition.

The process that causes the program to freeze is invoked with;

subprocess.call('path _ filename _ options',shell=False)

1

There are 1 answers

3
Jakob Bowyer On

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.

Written by Alex Martelli

Also this for people without pywin32.

import ctypes
print ctypes.windll.shell32.IsUserAnAdmin()