Executing Command Prompt Commands from Python
Python is a high-level, interpreted programming language that is used for data analysis, artificial intelligence, machine learning, and web development. It is a powerful language that is easy to use and has a wide range of capabilities.
One of the capabilities of Python is its ability to execute Command Prompt commands. In this article, we will explore two methods for executing Command Prompt commands from Python and provide an example of how to display the current date in Command Prompt.
Method 1: CMD /K to execute a command and remain
The CMD /K method allows the command to execute and remain open, which makes it useful when the user wants to execute multiple commands in the Command Prompt. To execute a Command Prompt command using CMD /K in Python, use the following code:
import os
os.system("CMD /K command")
In this code, “command” is the Command Prompt command that the user wants to execute.
Method 2: CMD /C to execute a command and terminate
The CMD /C method allows the command to execute and terminate, which makes it useful when the user wants to execute a single command in the Command Prompt.
To execute a Command Prompt command using CMD /C in Python, use the following code:
import os
os.system("CMD /C command")
In this code, “command” is the Command Prompt command that the user wants to execute.
Example using CMD /K method to execute a command and remain
To display the current date in Command Prompt using CMD /K, use the following code:
import os
os.system("CMD /K date")
In this code, “date” is the Command Prompt command that displays the current date.
Executing Multiple Command Prompt Commands from Python
Python provides a convenient way to execute multiple Command Prompt commands in one line of code. The ‘&’ symbol is used to separate multiple commands. This method is useful for executing a sequence of commands that have to be run sequentially.
To execute multiple Command Prompt commands from Python, use the following code:
import os
os.system("CMD /K command1 & command2 & command3")
In this code, “command1”, “command2”, and “command3” are the Command Prompt commands that the user wants to execute. The ‘&’ symbol is used to separate the commands.
Example using CMD /K method to Change Color of Characters and Display Current Date
To change the color of characters and display the current date in Command Prompt using CMD /K, use the following code:
import os
os.system("CMD /K color 01 & date")
In this code, “color 01” is the Command Prompt command that changes the color of characters to blue and black, while “date” is the Command Prompt command that displays the current date. The “color” command in Command Prompt is used to change the color of the background and characters. The syntax of the “color” command is “color <background><characters>”. The background and characters are represented by a number between 0 and 9. The following table shows the color codes for the “color” command:
Color Code | Background Color | Characters Color |
---|---|---|
0 | Black | Black |
1 | Blue | Blue |
2 | Green | Green |
3 | Aqua | Aqua |
4 | Red | Red |
5 | Purple | Purple |
6 | Yellow | Yellow |
7 | White | White |
8 | Gray | Gray |
9 | Light blue | Light blue |
In the code provided, “01” represents the characters’ color to be blue, while the background color is black. The “date” command in Command Prompt displays the current date in the YYYY-MM-DD format. By combining the “color” and “date” commands in CMD /K, we can change the color of the characters and display the current date in one line of code.
Using CMD /C Method to Execute a Command and Terminate
Python provides multiple methods to execute Command Prompt commands, including the CMD /C method, which executes a command and terminates the Command Prompt window. This method is useful when conducting simple tasks that only require the execution of a single command.
To execute a Command Prompt command using CMD /C in Python, use the following code:
import os
os.system("CMD /C command")
In this code, “command” is the Command Prompt command that the user wants to execute. After executing the command, the Command Prompt window will be terminated.
The CMD /C method is useful for executing simple tasks that only require one command to be executed.
Example Using CMD /C Method to Change Color of Characters
To change the color of characters in Command Prompt using CMD /C, use the following code:
import os
os.system("CMD /C color 01")
In this code, “color 01” is the Command Prompt command that sets the text color to blue and the background color to black.
Customizing the Command Prompt Legend
The Command Prompt legend provides a quick reference for commonly used commands in Command Prompt. By default, it includes a list of basic commands that can be executed in the Command Prompt environment. However, if the user uses Command Prompt frequently and has developed custom commands, it may be helpful to include them in the legend.
To customize the Command Prompt legend, use the following steps:
- Open the Command Prompt window and type the following command:
Copy
doskey /macrofile=pathfilename.txt
In this code, “pathfilename.txt” is the path and name of the file where the user wants to store the commands.
- The user can then add custom commands to the file in the following format:
Copy
command=Command Prompt command
In this code, “command” is the name of the custom command, while “Command Prompt command” is the command that the user wants to execute.
- Save the file and close it.
- To display the custom commands in the Command Prompt legend, type the following command:
Copy
doskey /macros
This will display all the custom commands that the user has added to the file, along with the corresponding Command Prompt commands.
Conclusion
Python’s ability to execute Command Prompt commands provides additional flexibility when working in the Command Prompt environment. By using the CMD /K and CMD /C methods, as well as the ‘&’ symbol to separate multiple commands, users can execute complex commands and programs, while also customizing the Command Prompt legend to include frequently used commands for increased productivity.
By mastering these methods, users can improve their productivity and experience while using Command Prompt.