Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run cmd command in Python with admin

I want to run a windows command with Python 3. Like this os.system("echo hi"). However, How about running a command that requires admin access? How do you do this? Thanks.

like image 471
Luke Dinkler Avatar asked Aug 28 '14 20:08

Luke Dinkler


1 Answers

You can do this with the ShellExecuteEx Win32 API wrapper included in the Pywin32 extensions. If you are using something like ActivePython you may already have the extensions.

To use ShellExecuteEx :

import win32com.shell.shell as shell
commands = 'echo hi'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)
like image 158
snowcrash09 Avatar answered Oct 10 '22 01:10

snowcrash09