Execute a Command Prompt Command from Python

Need to execute a Command Prompt command from Python?

If so, depending on your needs, you may use either of the two methods below to a execute a Command Prompt command from Python:

(1) CMD /K – execute a command and then remain:

import os
os.system('cmd /k "Your Command Prompt Command"')

(2) CMD /C – execute a command and then terminate:

import os
os.system('cmd /c "Your Command Prompt Command"')

Still not sure how to apply the above methods in Python?

Let’s then review few examples to better understand how to execute a Command Prompt command from Python.

Methods to Execute a Command Prompt Command from Python

Method 1 (CMD /K) : Executing a command and then remaining

To see how to apply the first method in practice, let’s review a simple example where we’ll execute a simple command in Python to:

  • Display the current date in the Command Prompt
  • The Command Prompt will remain opened following the execution of the command

You may then apply the following code in Python to achieve the above goals:

import os
os.system('cmd /k "date"') 

Once you run the code in Python, you’ll get the date in the command prompt:

Current Date in the Command Prompt

Now what if you want to execute multiple command prompt commands from Python?

If that’s the case, you can insert the ‘&’ symbol (or other symbols, such as ‘&&’ for example, depending on your needs) in between the commands.

For example, what if you want to display all the characters in the command prompt in green and display the current date?

You can then use the following syntax in Python:

import os
os.system('cmd /k "color a & date"')

You’ll now see the current date displayed in green:

Execute a Command Prompt Command from Python

Note that for more complex commands, you may find it useful to run a batch file from Python.

Method 2 (CMD /C) : Executing a command and then terminating

For this method, you can execute the same commands as reviewed under the first method, only this time the Command Prompt will be closed following the execution of the commands.

For example, you may apply the following code in Python to change the color of all characters to green:

import os
os.system('cmd /c "color a"')

In this case, the command will still get executed, but you may not be able to see it on your monitor.

In general, you can get a useful legend with further information by typing the command below in the Command Prompt:

cmd /?

Graphical User Interface (GUI) to Execute a Command Prompt Command

In the final section of this guide, I’ll share the code to launch a simple tkinter GUI that will allow you to execute your command prompt commands using a single click!

Here is the code that you may use in Python to display the current date in green:

import os
import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'gray90', relief = 'raised')
canvas1.pack()

def myCmd ():
    os.system('cmd /k "color a & date"')
     
button1 = tk.Button(text='      Run Command      ', command=myCmd, bg='green', fg='white', font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=button1)

root.mainloop()

Once you run the code, you’ll see this GUI:

tkinter GUI to run Command Prompt Command from Python

Click on the ‘Run Command’ button, and you’ll then get the current date in green:

Execute a Command Prompt Command from Python

You may modify the code to execute other commands based on your needs.