Loading video player…

Getting Started With the subprocess Module

Real Python resources mentioned in this lesson:

00:00 In the previous video, you were given a quick overview of the subprocess module. In this video though, you’ll dive into some code and learn the basic usage of the subprocess module.

00:13 In step one, you’re going to create a simple timer in Python, and then in step two, you’re going to run the simple timer as normal. So from the terminal, there’s nothing new so far.

00:24 But in step three, you’re going to run this simple timer, not from the terminal, but from within another Python application. And that’s of course what you will need subprocess for.

00:36 Now, you can download the code or you can choose to write it from scratch. That is up to you. So depending on your choice, go to your favorite IDE and either open or create a new file called simple_

00:49 timer.py.

00:53 And this is the code. This timer doesn’t do very much. It just starts by printing this line of code here. It then loops five times, and every time it loops, it prints a dot.

01:05 And because we set end to being empty, it will print the five dots on the same line. We flush the buffer, and then we wait one second before we go to the next iteration in the loop.

01:18 And at the end of that, it will print Done.

01:22 Now you might have noticed that the underscore here, we are using an underscore as a loop variable here because the loop variable isn’t being used in this loop.

01:31 Now, if that is not entirely clear to you, or if you want to find out more about the use of underscores in Python, I have included a link to a very good video course, which incidentally was done by me.

01:44 So that explains everything you need to know about the use of underscores in Python.

01:51 Okay, so that was step one. You have created your simple timer. Step two, let’s run this timer as normal from the terminal. So go to your terminal,

02:02 and make sure that you are in the right folder or the right directory, so where this .py file is saved. And as always, you type python, or if you are on Mac or some Linux distros, that will be python3.

02:16 But in my case, it’s python. And then simple_timer.py and hit Enter. So it prints starting timer of five seconds. You see the five dots one every second, and then it prints Done, and that is the end of it. So that was step two.

02:34 That was just to show you what this timer actually does. So now step three is we’re going to run this timer from within another Python application, and that we will cover in the next lesson.

Become a Member to join the conversation.