简体   繁体   中英

How to know if a python script is running with admin permissions in windows?

I want to check if a python script is running with admin permissions on windows, without using the ctypes module. It is important for me not to use ctypes for some reasons.

I have looked with no luck.

Thanks.

You could try opening a file that requires admin permissions, here's a sketch of how you might do that:

file_requiring_admin_permissions = "C:/Windows/WindowsUpdate.log"
try:
    with open(file_requiring_admin_permissions, 'w'):
        print("Opened OK")
except PermissionError:
    print("Permission error, not admin")
except IOError as e:
    import os
    print(os.strerror(e.errno))

如果安装pywin工具集,则可以使用函数win32.shell.IsUserAnAdmin()来查看您是否为administrators组的成员。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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