How to run a Python file in CMD
Introduction
Today, let us learn about how to run a Python file using cmd. Cmd is nothing but command prompt. So first let us know how to run a Python code first.
We can run a Python in different ways like
- In the PYTHON IDLE Window by pressing RUN
- In the PYTHON Command Line Window
- In the System (or) Command Prompt Window
To run a program let us a create demo Python program to run it in different places.
EXAMPLE:
File name: Demo.py
'''
Writing a program for running a program in cmd
The demo program is about sorting of numbers
With using sort () method and without using sort method
'''
# Take a list with random numbers
l= [9, 1,4,6,0]
print("Before Sorting")
print(l)
print("Using Sort method")
l.sort()
print("After Sorting")
print(l)
print("-----------------------------------------------------------------------")
l=[9,1,4,6,0]
print("Before Sorting")
print(l)
print("Without Using Sort method")
for i in range(len(l)):
for j in range(i,len(l)):
if(l[i]>l[j]):
l[i],l[j]=l[j],l[i]
print("After Sorting")
print(l)
Output:
'''
Writing a program for running a program in cmd
The demo program is about sorting of numbers
With using sort () method and without using sort method
'''
# Take a list with random numbers
l= [9, 1,4,6,0]
print("Before Sorting")
print(l)
print("Using Sort method")
l.sort()
print("After Sorting")
print(l)
print("-----------------------------------------------------------------------")
l=[9,1,4,6,0]
print("Before Sorting")
print(l)
print("Without Using Sort method")
for i in range(len(l)):
for j in range(i,len(l)):
if(l[i]>l[j]):
l[i],l[j]=l[j],l[i]
print("After Sorting")
print(l)
So now we know the output of the program. Let us know how to use the command prompt now.
How to Open Command Prompt?
We can open command prompt in two ways. One way is typing command prompt in the search bar.
After typing command prompt in the search bar, press enter key or click on the command prompt icon. Then the command prompt window is opened.
The other way is giving the command windows key+R. Then a popup like this opens up.
Then type cmd in the given box. Then a window with black screen opens. That window is called as COMMAND PROMPT.
The below black screen how a command prompt window looks like.
Functions in a Command Prompt
1) mkdir( make directory)
By using this command in the command prompt a directory is created in the directory in which we are present.
Here it is called directory in computer terms. In general terms directory is also know as a folder
2) dir(directory)
By using this command in the command prompt we can view all the contents of the directory at which our prompt window is present now.
Example:
We are at C directory now.
Now enter a command dir.
Now we can see all the contents of C directory.
Let us see how a program can be runned in a command prompt window. There are steps to be followed for running a program on cmd.
- Open command prompt window
- Set the path toward the residence of the required file.
- After reaching the required file give a command-python file_name.py
- Now the output is appeared.
This is how a program is runned on a command prompt.
Let us apply these steps and get the output of the program.
To apply these steps let us know the location of the file.
Location of Demo.py is:
C:\Users\v reddy\AppData\Local\Programs\Python\Python39
To reach to Python39 we use a command called cd. cd means change directory.
Syntax:
Syntax of cd is:
cd directory name
Then after giving the directory name press enter.
Note: The directory can only be changed if and only if the directory is present in the present directory.
The commands in the command prompt:
C:\>cd Users
C:\Users>cd v reddy
C:\Users\v reddy>cd AppData
C:\Users\v reddy\AppData>cd Local
C:\Users\v reddy\AppData\Local>cd Programs
C:\Users\v reddy\AppData\Local\Programs>cd Python
C:\Users\v reddy\AppData\Local\Programs\Python>cd Python39
C:\Users\v reddy\AppData\Local\Programs\Python\Python39>
Only the underlined the content only must be written on the command prompt window. This is how we can reach our file destination in command prompt window.
Now let us give a command Python (space) file name.py.
This command shows me the output of the program.
The file name in which we stored the above written program is demo.py
The command given is Python demo.py
Code:
Users\v reddy\AppData\Local\Programs\Python\Python39>python demo.py
Before Sorting
[9, 1, 4, 6, 0]
Using Sort method
After Sorting
[0, 1, 4, 6, 9]
-----------------------------------------------------------------------
Before Sorting
[9, 1, 4, 6, 0]
Without Using Sort method
After Sorting
[0, 1, 4, 6, 9]
Output:
This is how the output of the program looks inside a command prompt.
This is all about running a Python file in command prompt.