Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Opening CMD from Python
#1
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...
Reply
#2
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'
Reply
#3
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.
Oshadha likes this post
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is IDLE not opening on a MacOS Montery 12.6 and using Python 3.10.7? Merlin385 7 1,848 Oct-08-2022, 08:36 PM
Last Post: Merlin385
Question Opening small size browser with python selenium not work, need help greenpine 0 1,670 Feb-07-2022, 11:36 AM
Last Post: greenpine
  python keeps opening script directory Tyrel 5 3,137 Aug-06-2021, 10:43 PM
Last Post: snippsat
  Python OpenCV window not opening in fullscreen mode Zman350x 0 3,384 Apr-29-2021, 07:54 PM
Last Post: Zman350x
  cv2.imshow Python is not opening the window on top Petko 0 2,447 Feb-21-2020, 11:11 AM
Last Post: Petko
  Python opening a new command window m_lokeshkumar 1 1,918 Jan-05-2020, 09:01 PM
Last Post: Larz60+
  Error -3 when opening matlab file in python python_newbie09 0 2,351 Aug-02-2019, 11:51 AM
Last Post: python_newbie09
  python script file not opening in IDLE editor srm 2 4,456 Jun-23-2019, 08:45 AM
Last Post: Larz60+
  Opening a livestream in python JosefFilosopio 3 3,864 Mar-06-2019, 04:11 PM
Last Post: Axel_Erfurt
  opening a file from a windows desktop folder- error opening file Charan007 1 3,036 Dec-06-2018, 11:50 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020