def run_as_admin(extra_args=None):
"""If required, rerun the script and request admin privileges.
:params iterable extra_args:
Additional arguments to pass to the script in case it has to be
restarted.
"""
if not am_admin():
try:
print('Restarting and requesting admin privileges.')
args = sys.argv
if extra_args:
args = args + extra_args
exe, args = sys.executable, ' '.join(args)
if sys.version_info[0] == 2:
exe = _decode(exe)
ctypes.windll.shell32.ShellExecuteW(None, 'Runas', exe, args, None, 1)
sys.exit()
except Exception as e:
print(e)
msg = ('Unable to elevate privileges. You need to rerun the script'
' with Administrator privileges yourself. E.g. try pressing'
' the Windows key + x, then select "Command Prompt (Admin)"'
' and run the script in that console.')
sys.exit(msg)
评论列表
文章目录