Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Understanding the CompletedProcess Object

00:00 In this video, you’ll get to grips with the CompletedProcess object that we briefly mentioned earlier. So you’ll remember the code import subprocess, and then subprocess.run() with our list there.

00:14 ["python", "simple_timer.py"]. When you hit Enter, the timer starts for five seconds as we’ve seen before. The five dots, the word done, and then there is the CompletedProcess line here.

00:29 So what happens is that the run() function returns a CompletedProcess object and it seems to have args to arguments and also a return code.

00:41 But I would like to find out what else this CompletedProcess object has to offer. So what I’m going to do, I’m going to go to this line here and I’m going to press the up arrow, which brings back the latest line of code.

00:56 And instead of running it like this, I’m going to assign this to a variable called cp, which is completed process, and now I’m going to click Enter.

01:07 Same thing happens, a simple timer will run.

01:11 And when the word Done appears no longer is there this CompletedProcess line, it’s gone. But that’s a good thing because I have assigned it to cp.

01:21 So now I can investigate cp. And how do we do that? Well, let’s use the dir() function. Ah, so there’s quite a lot of interesting stuff.

01:32 There’s quite a few dunder methods, so double underscore methods. Again, if you don’t know what they are, please look at the video that explains the use of underscores in Python.

01:42 But towards the end you will recognize args, you’ll recognize returncode, and there are a few other things in there. There’s check_returncode(), and then there is stderr, stdout.

01:53 And these are two output streams that we will investigate later in the course. But for now, let’s have a look at the args, for example. So cp.args,

02:05 and that brings back this list, which is of course, the list you’re seeing here as well. args equals that, and if I ask for the returncode, I expect to return a 0.

02:16 So cp.returncode,

02:21 and indeed I get 0.

02:24 A return code of 0 is a code that is returned when the process ran without error. Anything other than zero, that is usually bad news because that means something went wrong.

02:37 And that is what we’ll be looking at in the next video when we talk about exceptions.

Become a Member to join the conversation.