How to Run Python Using CMD (Command Prompt) – Working in Windows 7,8,10
Python is one of the most used programming languages in 2021. Knowing How to Run Python Using CMD (Command Prompt) is a basic skill you need in order to run the python script.
In this tutorial, I am gonna show you How to run Python script as Python command line.
To begin with, you should know how to open CMD on your computer.
So, before moving on how to run python file in terminal, let us learn some of the basic commands useful in working with command prompt.
#1. How to Open Command Prompt in a Folder
Switiching from one location to another location in CMD may appear a duanting task for someone not familar with Command Prompt. However, it need not be. There is a simple and easiest way to open any folder or directory in cmd easily.
In the image below, I have created a folder called aiinspired.com in my desktop. Now, I want to open that folder in CMD without having to follow the usual procedure to get into the folder.
To open any folder in CMD,
Step 01: Open the folder that you want to open in Command Line.
Step 02: Once you open the folder, check the image below. Like shown in the image below, Click the Address Bar and type the keyword CMD and hit enter.
That’s it. Now CMD command prompt would be open up with the address of your folder (See image).
Now you are in the folder or directory that you wanted CMD to open in.
#2. How to change directory in cmd (Windows 10 and Other OSs)
Though #1 is really really useful, sometimes you will have to change from one directory to another directory when working in CMD. Therefore, In order to go to a directory insde the directory, You can simply use the command cd (refers to Change Directory) followed by the folder name.
For instance, in below image, I want to go to the directory (folder) test which is inside the directory aiinspired.com. Now using the Method #1, I am already in aiinspired.com in CMD.
In order to do that, I have to type cd test (cd [space] folder/directoryName).
#3. How to return to previous directory in cmd
Sometimes, you will have to move upword direction. Meaning from previous directory to next level. However, How to go back a directory in terminal windows can be done in several ways. Let’s see one by one.
#3.1 To Navigate into the root directory
To Navigate into the root directory regardless of where you are currently in, use the command cd / (cd [space] forward slash) and hit enter.
Below Image shows the output of this command.
#3.2 To Navigate one Directory or level Up
If you want to move one level up you can simply use the command cd.. (cd period period without space) as shown below
#4. How to list all files in a directory in cmd
This is one important part of CMD where you should know how to list all the files in a ditectory or folder. It often happens that when you excute a python file using CMD, sometimes you will be thrown a message mentioning No such file or directory. It is because there is either no file or directory in the name you have mentioned or typed. In instances like this, we will have to list out all the files in a directory.
To do that simply use the command dir. This would list all the files reside inside a directory as shown in the image below.
#5. How to Use Autocomplete feature in CMD
This is another feature in command prompt. You do not need to type file name or director. With the necessary command, you can simply use the Tab key from your keyboard. It will automatically display the directory or file name in alphabetical order. So, you can simply hit enter.
Check out the image below to know how it works
#6. How to clear command prompt history in CMD
Sometimes, CMD will be messed with lot of commands and you will have to navigate back and forth. So, having an option easily clear the screen of cmd will be useful.
To do that just use the command cls (clear screen). This will clear up all the previous working of the CMD screen. Check the image below
#7. How to Run Python Using CMD
Now, we have come to the conclusion of this article. I hope you are confident and skilled in using cmd by now.
Now if you want to run your python file, simply use the command python —filename.py
In the above image,
I have created a python file to print AIINSPIRED.COM with the following code
The file name is myfile.py
We hope now that you know How to Run Python Using CMD and You are confident in working with command prompt enviroment for performing basic operations.