Heyy there, Welcome to another lesson from Noob Code Pro!! In this lesson we will create a batch file to run a python program. 

The whole thing is a simple two-step process, so without wasting any more time let’s get right into the lesson:

  1. CREATING A PYTHON PROGRAM:

Obviously in order to create a batch file to run a python program, you need a python program to start with. So, I’ll be using a simple input output program for this lesson so that you can see both sides of execution. 

  1. Create a new py file by right clicking on your desktop and creating a new text document and then save it the file it with ‘.py’ extension.

  2. Open this new py file with your Python IDLE or any other IDE (I’ll be using the IDLE for this lesson)

  3. Now, type in the following code:

n = int(input("Input a number: "))

if n % 2 == 0:

print(n, " is an even number!")

else:

print(n, " is not an even number!")

This is a basic program that takes input from the user and checks if the inputted number is an even number or not.

Our python program is ready to go!!! Now all we need is a batch file.

  1. CREATING A BATCH FILE TO EXECUTE THE PYTHON PROGRAM:

Batch files are also called ‘Shell Scripts’ in other operating systems. They basically let you run multiple commands by just running the batch file. A Batch file is just a text file with commands saved to a file that ends with ‘.bat’.

Creating a batch file is easy! It’s basically a file that executes commands in your system, more or less like a python file but a batch file can and is meant to actually make changes to your system. We can create a batch file using notepad just like we created our python file. Just open Notepad like before and create a new file, but this time, save it with a ‘.bat’ extension to create a batch file.

Type in the following commands:

@py <filepath> %*

@pause

Remember the python file we created in the first step? Replace ‘<filepath>’ in the above commands with the file path of that python file (in quotes). 

In the above command, the ‘@’ symbol tells Windows that it doesn’t need to display the command that follows, just execute it. The ‘%*’ symbol tells Windows to forward any command-line arguments (refer to the last section of this article for more details) to the Python Program.

Now just run the batch file to the run the python program.

You can use ‘pyw’ instead of ‘py’ in the batch file to stop the command-line window from appearing when the batch file is executed. 

WHAT ARE COMMAND-LINE ARGUMENTS?

Command-line arguments are typed when you run your python program. These arguments are typed after the file name of the program you want to run, separated by space.

These command-line arguments are stored in the program in a variable called ‘sys-args’ which can be called by importing the ‘sys’ module in the python program and printing ‘sys.args’.

CONCLUSION:

And that’s how you use a batch file to run a python program. Knowing this can be really helpful as batch files are really essential when it comes to working with an OS like Windows. We will also be needing this knowledge to execute programs like our virtual assistant, which we have been working on as well. If you haven’t started with your virtual assistant, here is the first article to Creating Your Own Virtual Assistant. 

If you have any doubts, queries or questions, comment below or chat with me and other programmers directly on our telegram community, we would love to have you there

If you found this article helpful, give it a like and share it with your coding  buddies!!! 

I’ll see you next week with a new lesson to share with you, until then….

   HAVE A GREAT DAY PEOPLE !!!