How To Open Command Prompt Using Python

Opening command prompt using python
Table of Contents

Today we will see how to open command prompt using python you may want to create a software which requires you to run commands in command prompt or you can use for automation process.

I will show you how to open command prompt using python it is very simple you will also be able to run commands in command prompt by using python you don’t need to install any libraries.

If you want to create projects than you check out my projects you can get the source code for free you can visit coderzway projects here

Let’s open command prompt using python

For this you need to have python installed you must have installed python but if you don’t have than you can download and install python from here – download python here.

Now you need to create a python file with .py extension and open it in a code editor you can use vs code which is really awesome you can download vs code from here – download vs code.

We will be using os module from python it is a builtin module which comes with python you need to import os module first

Than write this code to open command prompt

				
					os.system("start cmd")
				
			

This code will start a new command prompt you can also open notepad using python you need to replace cmd with notepad it will be start notepad this will open new notepad.

So now you have seen how to open command prompt using one line of python code now you may want to write and execute in the command prompt.

To write something in the command prompt write this code

				
					 # Enter the name of the command you want to execute
os.system("start cmd /k name-of-the-command")
#like this
os.system("start cmd /k help")

				
			

You need to type name of the command you want to execute this will open the command prompt and run the command you can use it to create folder run a program and many more you can open multiple command prompts by writing another line of the code.

If you want to automatically close the window after execution of the program than you can use this code

				
					os.system("start cmd /c help")
				
			

You just need to enter /c instead of /k this will open, run and close the command prompt.

So this was how to open command prompt using python it was just one line of code you can do automation like creating project folder or installing or running program.

Thank you for reading this article I hope you found what you were looking for if you want more articles like this than subscribe to our newsletter you will get the latest updates and guides thank you.

Share This Post
Share on facebook
Share on twitter
Share on email
Share on whatsapp

Leave a Reply

Subscribe To Our Newsletter

Get updates and learn from the best

Latest Guides & Articles