I'm able to run commands through CMD with Python with subprocess.call('insert command here') but it opens the CMD window then closes right away. Is there a way to not open it at all?
You can set shell=True when calling subprocess.call:
subprocess.call('insert command here', shell=True)
Related
in my script, i call an app from os.system()
command = f"python -m snakeviz {filename}"
os.system(command)
the problem is that this application is freezing the console, therefore freezing my python plugin.
we cannot input anything, ("exit" command won't work)
the only way to close the terminal is by running CTRL+C shortcut, and the terminal might be hidden..
this app has no reason to keep running it just open a webpage,
how can I force quit the command, without losing the rest of my script?
Whatever i did, the command froze my python plugin
I needed to open another process with Open()
and need shlex to encrypt the command line to whatever this subprocess module need
command = f"python -m snakeviz myfile.log"
subprocess.Popen(shlex.split(command))
I run Windows 10 and I'm trying to execute linux sommands in CMD.
I have installed ubuntu so that I can run bash in CMD
Now I want to be able to run commands in this CMD window.
In this case a simple ping command.
First. I can not figure how to reuse the CMD window, I don't want to open a new CMD for every loop.
Second. I want to make sure that the command is done before executing the next in the loop.
cmd_shell = subprocess.Popen('start cmd /K bash', shell=True)
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = "ping -c4 "+site
# this is where I need help to execute the ping command in the already opend CMD window
After I have open and executed bash, I cant find a good way to write in the CMD window.
I did a not so nice solution using pyautogui but I think there's a much better way to do it, but I dont know how.
Help please.
/ BR BaconFlip
So I realized that I did not need a CMD window to do this.
I could just run it in the script like this:
sites = ["google.com", "python.org"]
for site in sites:
cmd_line = '"ping -c4 '+site+'"'
command = check_output('c:\\Windows\\System32\\bash.exe -c '+cmd_line)
print(command)
Is possible to manipulate cmd window from python without closing it everytime I send a new command?
I would like to manipulate it as I do with keyboard, command by command, with command prompt opened, so I could see outputs.
For now I'm using this, but is not what I want...
subprocess.Popen('start dir', shell=True)
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.
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"])