Python WIN 10: Run WMIC in shell with ADMINISTRATOR priviledges - python

I'm trying to run the following in a shell. It doesn't work though. The WMIC command has to be run as an administrator. How to I use subprocess.run as an Administrator?
import subprocess
subprocess.run('wmic product where name="Application I Wish To Uninstall" call uninstall', shell=True)

Did you try this?
start your command prompt as administrator
run your python script like "python mypythonapp.py"

Related

How do i run python file in cmd from vscode

I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html

how to write command in command prompt (cmd) in windows 10 from python script with subprocess module

I need to launch external program from python script, which can be run in command prompt. I've been searching through python documentations and stack overflow but can't find anything helpful to me. I successfully launch cmd with following script:
But I still need to write down more commands like that:
mkdir data
copy data.txt c:\data
I think this is a very easy job with subprocess module but I can't find the way. How can I do this?
Try to use call
subprocess.call(['cmd.exe', 'mkdir data']);

How to run python script with depentdent shell command in the background

Is it possible to run shell command at background in python script? Let say I have run.py and in this script I need to do,
#run shell command(in python script until script is finished)
#continue with script
But the problem is that they are dependent. To run my script I need to run shell command in the same script.
Yes, you can use the subprocess library in Python and check the status of the output.
Ex: subprocess.check_call("exit 1", shell=True)

Launch Python script in new terminal

I want to launch a python script in a new macOS terminal window from another script. I'm currently using this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python'])
which launches a python prompt in a new terminal window.
But when I try to run a python script with this code:
subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python', 'test.py'])
it completely ignores the 'test.py' on the end and starts a python prompt, just like it does without the test.py on the end.
How can I make this work?
Don't use the open -a terminal.app, just use the python executable
subprocess.call(['/usr/bin/python', 'test.py'])
This Python code will open a new terminal window, and then have python3 run test.py:
import os
os.system("""osascript -e 'tell application "Terminal" to do script "python3 test.py"'""")
I'm unable to come up with a way to get this into a subprocess.call() call.

Python: Executing the windows command in windows command prompt from python script

I have to execute python script in windows command prompt
I am using the following command to run the command, so that the script opens the command prompt execute it
os.system("start /wait cmd /c {c:\\python27\\python.exe C:\\examples\\xml2html.py --dir c:\\Temp\\abcd c:\\tmp\\results.xml}")
I will be expecting a new directory called "abcd" created at that location and some output files created inside that.
When I run this command normally in the windows command prompt it works. I am not able to execute this in the script. Windows command prompt opens and terminates quickly.
Could any one let me know where exactly is it going wrong with the command please?
Unless you want to open a new console window you don't need to run cmd.exe (%COMSPEC%) in order to run another Python script as a subprocess:
import sys
from subprocess import check_call
check_call([sys.executable, "C:\\examples\\xml2html.py",
"--dir", "c:\\Temp\\abcd", "c:\\tmp\\results.xml"])

Categories