Python Forum
Opening CMD from Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Opening CMD from Python (/thread-37740.html)



Opening CMD from Python - Oshadha - Jul-16-2022

I want to open a cmd terminal at a specific directory, and run a command, then get the output to a variable in python. I searched the internet but could not find a suitable solution.
Thanks...


RE: Opening CMD from Python - snippsat - Jul-16-2022

Use subprocess.
To make a working example open imcat in a specific folder(cwd) and catch the output.
import subprocess

response = subprocess.run(['imcat.exe', 'test.jpg'], capture_output=True, cwd=r'C:\cmder\bin')
print(response.stdout)
Output:
b'\x1b[38;2;198;217;220m\x1b[48;2;210;224;226m\xdf\x1b[38;2;130;169;183m\x1b[48;2;129;169;184m\xdf\x1b[38;2;88;128;143m\x1b[48;2;72;119;136m\xdf\x1b[38;2;66;95;107m\x1b[48;2;54;88;102m\xdf\x1b[0m\r\n'



RE: Opening CMD from Python - Skaperen - Jul-17-2022

you don't need a terminal to run a command, in all the common operating systems, in all the common languages. see post #2 for Python.