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...
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'
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.