×

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.

How To Run A Python File In Cmd

The other way is giving the command windows key+R. Then a popup like this opens up.

How To Run A Python File In Cmd

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.

How To Run A Python File In Cmd

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.

  1. Open command prompt window
  2. Set the path toward the residence of the required file.
  3. After reaching the required file give a command-python file_name.py
  4. 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.

How To Run A Python File In Cmd

This is all about running a Python file in command prompt.


Related Topics

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Reverse a Number in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

4 minutes read.

Performing Transaction Using Python MySQL

Transaction A Transaction contains a set of SQL commands used to change the data in the dataset. If we say transaction, it means that the data in the database has changed....

3 minutes read.

Python SymPy

A Python package for symbolic mathematics is called SymPy. Its goal is to develop into a fully-fledged computer algebra system (CAS) while maintaining the code as straightforward as possible to...

3 minutes read.

Python ord() Function

Python ord() Function The ord() function in Python returns the number representing the Unicode code of a specified character. Syntax ord(c) Parameter c: This parameter represents a string or any character. Return This function returns the number...

1 minute read.

Speech Recognition in Python

What is Speech Recognition? Speech Recognition is a term defined for automatic recognition of human speech. Speech recognition is the most significant activity in the domain of the interaction between the...

8 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.

Python round() function

Python round() function The round() function in Python returns a number rounded to ‘ndigits’ precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input. Syntax round(number[, ndigits]) Parameter Number: This parameter represents the...

1 minute read.

How to Install Pandas in Python?

Pandas is a library created in Python for handling and analyzing data. Pandas provides a variety of procedures and data structures for manipulating time series and numerical data.  Pandas is...

3 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.

Python Set intersection() method

Python Set intersection() method The set.intersection() method in Python returns a new set with elements that are similar between two or more sets. Syntax set.intersection(set1, set2 ... etc) Parameter set1- This parameter represents the set to search for...

2 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Sklearn linear Model in Python

The most effective and reliable Python machine learning library is Sklearn. Through a Python consistency interface, it offers a variety of effective tools for statistical modeling and machine learning, including...

5 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.

Check Palindrome in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Python email utils

Python is a popular programming language in this growing world. There are many resources to learn python online without spending a single penny. In this article, we will talk about a...

4 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Data Structures and Algorithms using Python | Part 2

Files: A file is a location or information stored in computer storage devices. File handling is essential when the information or the data is to be held permanently. When we try to...

20 minutes read.