title
Python Tutorial: Calling External Commands Using the Subprocess Module

description
In this Python Programming Tutorial, we will be learning how to run external commands using the subprocess module from the standard library. We will learn how to run commands, capture the output, handle errors, and also how to pipe output into other commands. Let's get started... ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python

detail
{'title': 'Python Tutorial: Calling External Commands Using the Subprocess Module', 'heatmap': [{'end': 117.102, 'start': 89.076, 'weight': 0.827}, {'end': 489.914, 'start': 443.405, 'weight': 0.863}], 'summary': "This tutorial covers using python's subprocess module to call external commands, capture their output, and pipe output between commands, with examples of 'ls' and 'dir' in mac and windows environments. it also outlines the usage of subprocess.run in python, emphasizing security hazards, capturing standard output, and redirecting output to a file. additionally, it demonstrates handling external command output, using python to call external commands, and processing their outputs and inputs.", 'chapters': [{'end': 142.25, 'segs': [{'end': 25.277, 'src': 'embed', 'start': 0.249, 'weight': 0, 'content': [{'end': 6.317, 'text': "Hey there, how's it going everybody? In this video, we're going to be learning how to call external commands using Python with the Subprocess module.", 'start': 0.249, 'duration': 6.068}, {'end': 10.623, 'text': 'So there are many scenarios where you might want to call an external program using Python.', 'start': 6.658, 'duration': 3.965}, {'end': 17.152, 'text': 'And if you need to, then you can also capture the output of those commands or even pipe the output from one command into another.', 'start': 10.964, 'duration': 6.188}, {'end': 21.215, 'text': "So that's what we're going to be learning to do in this video using the subprocess module.", 'start': 17.472, 'duration': 3.743}, {'end': 22.896, 'text': "So let's go ahead and get started.", 'start': 21.515, 'duration': 1.381}, {'end': 25.277, 'text': 'So I have a blank Python module open here.', 'start': 23.316, 'duration': 1.961}], 'summary': 'Learn how to call external commands using python with the subprocess module.', 'duration': 25.028, 'max_score': 0.249, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y249.jpg'}, {'end': 117.102, 'src': 'heatmap', 'start': 59.068, 'weight': 1, 'content': [{'end': 64.688, 'text': 'So LS, the command that I just ran here, it will list the files and folders in the current directory.', 'start': 59.068, 'duration': 5.62}, {'end': 67.429, 'text': "And again, that's a Linux command on Windows.", 'start': 64.968, 'duration': 2.461}, {'end': 69.31, 'text': 'You have to use Windows commands.', 'start': 68.03, 'duration': 1.28}, {'end': 73.811, 'text': 'So the LS equivalent on Windows is dir, so D-I-R.', 'start': 69.49, 'duration': 4.321}, {'end': 74.731, 'text': 'Oops, sorry, D-I-R.', 'start': 73.911, 'duration': 0.82}, {'end': 84.154, 'text': 'So if I save this and run it, then we can see that the ls command printed out everything in the current directory.', 'start': 76.451, 'duration': 7.703}, {'end': 89.076, 'text': "Now, if you're on Windows and tried to run the dir command or something similar,", 'start': 84.514, 'duration': 4.562}, {'end': 93.558, 'text': 'then you might have got an error at this point that the system could not find the file specified.', 'start': 89.076, 'duration': 4.482}, {'end': 98.46, 'text': 'So the reason is because the dir command is built into the shell.', 'start': 93.958, 'duration': 4.502}, {'end': 101.844, 'text': "We'd have to pass in an argument of shell equals true.", 'start': 99, 'duration': 2.844}, {'end': 109.212, 'text': "If you're on Windows, then you could say, pass in an extra argument here of shell is equal to true.", 'start': 102.464, 'duration': 6.748}, {'end': 117.102, 'text': 'If we run that, we can see here that on Mac, we get the same result, but on Windows, that should prevent you from getting that error.', 'start': 109.272, 'duration': 7.83}], 'summary': "Using 'ls' command on windows requires 'dir' command with 'shell=true' argument to prevent errors.", 'duration': 42.776, 'max_score': 59.068, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y59068.jpg'}, {'end': 149.631, 'src': 'embed', 'start': 122.749, 'weight': 3, 'content': [{'end': 130.621, 'text': 'So if I wanted to add more arguments to my external command, then I could say, so for example, some arguments I could add to ls would be "-la".', 'start': 122.749, 'duration': 7.872}, {'end': 138.789, 'text': 'So if I run this, then now we can see that it runs that ls command with those additional arguments.', 'start': 131.062, 'duration': 7.727}, {'end': 142.25, 'text': 'And those additional arguments just provide more information here.', 'start': 139.169, 'duration': 3.081}, {'end': 149.631, 'text': "Now, if you are using this shell equals true, then that can be a security hazard if you're using untrusted input.", 'start': 142.65, 'duration': 6.981}], 'summary': 'Adding arguments to external command like ls with -la provides more information. using shell=true with untrusted input can be a security hazard.', 'duration': 26.882, 'max_score': 122.749, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y122749.jpg'}], 'start': 0.249, 'title': "Using python's subprocess module", 'summary': "Covers using python's subprocess module to call external commands, capture their output, and pipe output between commands, with examples of 'ls' and 'dir' in mac and windows environments.", 'chapters': [{'end': 142.25, 'start': 0.249, 'title': 'Python subprocess module: calling external commands', 'summary': "Covers the usage of python's subprocess module to call external commands, capturing their output, and piping the output from one command into another, with examples of 'ls' and 'dir' commands in a mac and windows environment.", 'duration': 142.001, 'highlights': ['The subprocess module in Python allows for calling external commands and capturing their output, with the ability to run commands and pipe their output into another command.', "The 'ls' command in a Mac environment lists the files and folders in the current directory, while the equivalent in Windows is 'dir'.", "On Windows, using the 'dir' command may require passing an argument of shell equals true to prevent errors.", "Setting the shell argument to true allows passing an entire command as a string, and additional arguments like '-la' can be added to the 'ls' command for providing more information."]}], 'duration': 142.001, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y249.jpg', 'highlights': ['The subprocess module in Python allows for calling external commands and capturing their output, with the ability to run commands and pipe their output into another command.', "The 'ls' command in a Mac environment lists the files and folders in the current directory, while the equivalent in Windows is 'dir'.", "On Windows, using the 'dir' command may require passing an argument of shell equals true to prevent errors.", "Setting the shell argument to true allows passing an entire command as a string, and additional arguments like '-la' can be added to the 'ls' command for providing more information."]}, {'end': 547.829, 'segs': [{'end': 188.765, 'src': 'embed', 'start': 142.65, 'weight': 0, 'content': [{'end': 149.631, 'text': "Now, if you are using this shell equals true, then that can be a security hazard if you're using untrusted input.", 'start': 142.65, 'duration': 6.981}, {'end': 152.832, 'text': "So only use that if you're passing in the arguments yourself.", 'start': 149.951, 'duration': 2.881}, {'end': 156.753, 'text': "And be sure you're not running it with user input or anything like that.", 'start': 153.272, 'duration': 3.481}, {'end': 163.735, 'text': "But if you're not using the shell argument, then we actually can't pass in the entire command as a string like we did here.", 'start': 157.133, 'duration': 6.602}, {'end': 167.336, 'text': 'Instead, we need to pass in everything as a list of arguments.', 'start': 164.135, 'duration': 3.201}, {'end': 173.478, 'text': 'So if we wanted to run this same command without the shell argument set to true, then we would have to say so.', 'start': 167.656, 'duration': 5.822}, {'end': 175.258, 'text': 'let me get rid of the shell argument here.', 'start': 173.478, 'duration': 1.78}, {'end': 182.381, 'text': "So I could just pass this in, as a list, and I'm going to have to do this as a list.", 'start': 175.658, 'duration': 6.723}, {'end': 186.404, 'text': 'So our first item here is going to be the command, ls.', 'start': 183.062, 'duration': 3.342}, {'end': 188.765, 'text': 'The second item here is the arguments.', 'start': 186.724, 'duration': 2.041}], 'summary': 'Caution when using shell=true, prefer passing arguments as a list', 'duration': 46.115, 'max_score': 142.65, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y142650.jpg'}, {'end': 232.97, 'src': 'embed', 'start': 206.554, 'weight': 3, 'content': [{'end': 212.676, 'text': 'the standard out just goes wherever the standard out of the script normally goes, which is in the console here.', 'start': 206.554, 'duration': 6.122}, {'end': 215.878, 'text': "Let's try to capture this into a variable instead.", 'start': 213.277, 'duration': 2.601}, {'end': 222.78, 'text': 'Now, you might try to do this just by saying something like p1 is equal to subprocess.run.', 'start': 216.258, 'duration': 6.522}, {'end': 228.765, 'text': "But if I run that, then we can see that it's still printing the standard out here to the console.", 'start': 223.38, 'duration': 5.385}, {'end': 232.97, 'text': "But also let's see if we get anything within that P1 variable.", 'start': 229.026, 'duration': 3.944}], 'summary': 'Capturing standard output into a variable using subprocess.run.', 'duration': 26.416, 'max_score': 206.554, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y206554.jpg'}, {'end': 302.546, 'src': 'embed', 'start': 275.571, 'weight': 2, 'content': [{'end': 287.391, 'text': 'So if I print out p1.returncode, if I run that, then we can see that we got a return code of 0, and that means that it ran successfully.', 'start': 275.571, 'duration': 11.82}, {'end': 291.855, 'text': 'now, a way that I like to remember that is to think of that as meaning 0 errors.', 'start': 287.391, 'duration': 4.464}, {'end': 295.579, 'text': 'now we can also grab the standard out as well.', 'start': 291.855, 'duration': 3.724}, {'end': 302.546, 'text': "so let's grab that if I print out p1.stdout and run that.", 'start': 295.579, 'duration': 6.967}], 'summary': 'The return code is 0, indicating successful execution, and the standard output can be accessed.', 'duration': 26.975, 'max_score': 275.571, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y275571.jpg'}, {'end': 489.914, 'src': 'heatmap', 'start': 411.602, 'weight': 4, 'content': [{'end': 416.085, 'text': "But in this case, when we decode those bytes, it's converting it into a string.", 'start': 411.602, 'duration': 4.483}, {'end': 427.871, 'text': "Now, if we don't want to use decode, then we can just get rid of that and pass in an argument to the run method saying that we want text instead.", 'start': 416.585, 'duration': 11.286}, {'end': 430.633, 'text': "So I'm going to remove the decode method there.", 'start': 428.312, 'duration': 2.321}, {'end': 434.977, 'text': "and I'm just going to pass in an additional argument of text equals true.", 'start': 431.093, 'duration': 3.884}, {'end': 442.965, 'text': "So now, if I run that, we can see that we're not decoding the standard out, but it comes in as a string anyway, so that's good.", 'start': 435.377, 'duration': 7.588}, {'end': 447.289, 'text': 'Okay, so when we set that argument of capture output equal to true,', 'start': 443.405, 'duration': 3.884}, {'end': 454.755, 'text': "What that's actually doing in the background is setting the standard out and the standard error to the subprocess pipe,", 'start': 447.769, 'duration': 6.986}, {'end': 456.756, 'text': 'and that allows us to capture those values.', 'start': 454.755, 'duration': 2.001}, {'end': 461.12, 'text': 'So let me show you what it would look like to set that standard out argument directly.', 'start': 457.117, 'duration': 4.003}, {'end': 472.168, 'text': "So instead of saying capture output is equal to true here, instead I'm going to say stdout is equal to, and that is subprocess.pipe.", 'start': 461.5, 'duration': 10.668}, {'end': 478.751, 'text': "And again, that's actually what setting capture output equal to true does in the background.", 'start': 474.23, 'duration': 4.521}, {'end': 482.412, 'text': 'But it also redirects the standard error to that pipe as well.', 'start': 479.071, 'duration': 3.341}, {'end': 489.914, 'text': 'But just setting the standard out equal to that subprocess pipe, we should be able to run what we have here and get the same results.', 'start': 482.972, 'duration': 6.942}], 'summary': 'Decoding bytes into string and capturing standard output using subprocess pipe.', 'duration': 45.154, 'max_score': 411.602, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y411602.jpg'}, {'end': 538.539, 'src': 'embed', 'start': 503.397, 'weight': 6, 'content': [{'end': 506.058, 'text': 'That could be used for logging or anything like that.', 'start': 503.397, 'duration': 2.661}, {'end': 511.841, 'text': 'Now in order to do that, we could simply just open up a file and redirect it to there.', 'start': 506.439, 'duration': 5.402}, {'end': 525.448, 'text': "So I can say with open and I will just open a file here called output.txt and I want to open that in write mode and I'll just open that as F.", 'start': 512.221, 'duration': 13.227}, {'end': 529.41, 'text': "Now let me indent this so it's within our context manager there.", 'start': 525.448, 'duration': 3.962}, {'end': 538.539, 'text': "And now for our standard route, instead of redirecting to this subprocess.pipe, instead, I'm just going to redirect that to F, our file.", 'start': 530.01, 'duration': 8.529}], 'summary': 'Demonstrates redirecting output to a file for logging purposes.', 'duration': 35.142, 'max_score': 503.397, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y503397.jpg'}], 'start': 142.65, 'title': 'Using subprocess.run and capturing standard output in python', 'summary': "Outlines the usage of subprocess.run in python, emphasizing the security hazard of using 'shell=true' with untrusted input, the need to pass arguments as a list, and the methods to capture and retrieve command outputs as well as checking the return code. it also explains capturing standard output in python using subprocess.run, decoding bytes to string, and redirecting standard output to a file.", 'chapters': [{'end': 348.985, 'start': 142.65, 'title': 'Using subprocess.run in python', 'summary': "Outlines the usage of subprocess.run in python, emphasizing the security hazard of using 'shell=true' with untrusted input, the need to pass arguments as a list, and the methods to capture and retrieve command outputs as well as checking the return code, with an example showing a return code of 0 indicating successful execution.", 'duration': 206.335, 'highlights': ["The security hazard of using 'shell=true' with untrusted input Using 'shell=true' with untrusted input can be a security hazard, so it should only be used when passing in the arguments oneself and not with user input.", 'The need to pass arguments as a list When not using the shell argument, passing in the entire command as a string is not possible; instead, everything needs to be passed in as a list of arguments.', "Retrieving command outputs and checking return code The chapter explains how to capture standard output by setting 'capture_output=true' and checking the return code using 'returncode', with a return code of 0 indicating successful execution."]}, {'end': 547.829, 'start': 348.985, 'title': 'Capturing and redirecting standard output in python', 'summary': 'Explains capturing standard output in python using subprocess.run, decoding bytes to string, and redirecting standard output to a file.', 'duration': 198.844, 'highlights': ['The subprocess.run command captures standard output in the variable, enabling it to be accessed and manipulated.', "Decoding bytes to string can be achieved by using the decode method or setting the 'text' argument to true.", "Setting the 'capture_output' argument to true in subprocess.run redirects standard output and standard error to a subprocess pipe for capturing.", 'Standard output can be redirected to a file by opening a file and redirecting the output to it.']}], 'duration': 405.179, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y142650.jpg', 'highlights': ['The need to pass arguments as a list When not using the shell argument, passing in the entire command as a string is not possible; instead, everything needs to be passed in as a list of arguments.', "The security hazard of using 'shell=true' with untrusted input Using 'shell=true' with untrusted input can be a security hazard, so it should only be used when passing in the arguments oneself and not with user input.", "Retrieving command outputs and checking return code The chapter explains how to capture standard output by setting 'capture_output=true' and checking the return code using 'returncode', with a return code of 0 indicating successful execution.", 'The subprocess.run command captures standard output in the variable, enabling it to be accessed and manipulated.', "Setting the 'capture_output' argument to true in subprocess.run redirects standard output and standard error to a subprocess pipe for capturing.", "Decoding bytes to string can be achieved by using the decode method or setting the 'text' argument to true.", 'Standard output can be redirected to a file by opening a file and redirecting the output to it.']}, {'end': 1130.12, 'segs': [{'end': 569.251, 'src': 'embed', 'start': 548.289, 'weight': 0, 'content': [{'end': 557.66, 'text': "So now, if I run this, it doesn't look like we got anything, but if I open up my sidebar here, we can see that now I have this output.txt.", 'start': 548.289, 'duration': 9.371}, {'end': 564.628, 'text': 'If I open that up, then we can see that we got the results from that command in our output.txt.', 'start': 558.081, 'duration': 6.547}, {'end': 565.988, 'text': 'file. so that did work.', 'start': 564.628, 'duration': 1.36}, {'end': 569.251, 'text': 'okay. so i wanted to show how we could redirect that to a file.', 'start': 565.988, 'duration': 3.263}], 'summary': 'Demonstrated command output redirection to file successfully.', 'duration': 20.962, 'max_score': 548.289, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y548289.jpg'}, {'end': 621.035, 'src': 'embed', 'start': 590.123, 'weight': 4, 'content': [{'end': 592.084, 'text': "So let's see what happens when we get an error.", 'start': 590.123, 'duration': 1.961}, {'end': 599.246, 'text': "So I'm going to change my command here to instead try to list the contents of a directory that doesn't exist.", 'start': 592.564, 'duration': 6.682}, {'end': 602.647, 'text': "So I'm just going to add a another argument here.", 'start': 599.606, 'duration': 3.041}, {'end': 607.388, 'text': "And I'm just going to add this to a bonus directory, I'll just say D and E for does not exist.", 'start': 602.867, 'duration': 4.521}, {'end': 614.392, 'text': "And this argument that ls command is going to try to list the contents of this that doesn't exist.", 'start': 608.169, 'duration': 6.223}, {'end': 621.035, 'text': "If I run this, then we can see that we don't get any output, and that's because we captured it here.", 'start': 614.552, 'duration': 6.483}], 'summary': 'Experimenting with an error - attempting to list contents of a non-existent directory.', 'duration': 30.912, 'max_score': 590.123, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y590123.jpg'}, {'end': 815.412, 'src': 'embed', 'start': 791.26, 'weight': 8, 'content': [{'end': 797.184, 'text': "So for example, let's say that we wanted to take the output from one command and have that be the input to another.", 'start': 791.26, 'duration': 5.924}, {'end': 799.506, 'text': "So let's look at an example of this.", 'start': 797.584, 'duration': 1.922}, {'end': 804.309, 'text': "So, for the first command, I'm just going to run a cat on a file,", 'start': 799.926, 'duration': 4.383}, {'end': 809.67, 'text': 'which is a Linux command that will just print the contents of a file if it just has one file as the input.', 'start': 804.309, 'duration': 5.361}, {'end': 815.412, 'text': "And for the second command, I'm going to use the output of that first command to grep that file.", 'start': 810.13, 'duration': 5.282}], 'summary': "Using linux commands, output from 'cat' is piped into 'grep'.", 'duration': 24.152, 'max_score': 791.26, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y791260.jpg'}, {'end': 879.541, 'src': 'embed', 'start': 848.391, 'weight': 1, 'content': [{'end': 853.956, 'text': "So in order to run the cat command on that file, I'm going to change this up a bit.", 'start': 848.391, 'duration': 5.565}, {'end': 864.166, 'text': "So instead of ls, I'm going to run cat and I want to run cat on that test.txt file in the same directory.", 'start': 854.236, 'duration': 9.93}, {'end': 870.954, 'text': "I don't have any more arguments after that, and I don't want to redirect standard error to dev null.", 'start': 864.767, 'duration': 6.187}, {'end': 879.541, 'text': "Instead I'm just going to set capture output equal to true so that we can get that output and any errors.", 'start': 871.354, 'duration': 8.187}], 'summary': 'Changing command from ls to cat and setting capture output to true for test.txt file.', 'duration': 31.15, 'max_score': 848.391, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y848391.jpg'}, {'end': 936.589, 'src': 'embed', 'start': 905.317, 'weight': 2, 'content': [{'end': 910.159, 'text': 'okay. so now, in order to use that output as the input of the grep command,', 'start': 905.317, 'duration': 4.842}, {'end': 916.041, 'text': "I'm going to copy what we have here previously and i'm just going to change this a little bit.", 'start': 910.159, 'duration': 5.882}, {'end': 928.566, 'text': "so let me paste this and now i'm going to change this to be p2 as our variable and now i want to run grep and i am going to do a couple of different arguments here.", 'start': 916.041, 'duration': 12.525}, {'end': 936.589, 'text': "so i'm going to also pass in an argument of dash n, which gives us the line number that it finds a match, just so we can test this.", 'start': 928.566, 'duration': 8.023}], 'summary': "Using grep command to search for a match with 'p2' as the variable, including the line number.", 'duration': 31.272, 'max_score': 905.317, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y905317.jpg'}, {'end': 1093.797, 'src': 'embed', 'start': 1027.051, 'weight': 3, 'content': [{'end': 1039.938, 'text': 'So what I mean by that is that you could, you know, instead of doing this whole process here, we could just say shell is equal to true.', 'start': 1027.051, 'duration': 12.887}, {'end': 1048.615, 'text': 'And then, instead of passing this in as a list of arguments, we could just say cat test.', 'start': 1040.598, 'duration': 8.017}, {'end': 1051.238, 'text': 'oops, I got to get rid of that square bracket as well.', 'start': 1048.615, 'duration': 2.623}, {'end': 1061.532, 'text': 'we could just pass all of this in as a string and then do a grep dash in test, and if I print out the standard out of that,', 'start': 1051.238, 'duration': 10.294}, {'end': 1063.275, 'text': 'then we can see that we got the same result.', 'start': 1061.532, 'duration': 1.743}, {'end': 1069.68, 'text': "But, like I was saying, it's definitely useful to know how to pass in the input to different external commands,", 'start': 1063.675, 'duration': 6.005}, {'end': 1076.946, 'text': 'because if you are doing step-by-step processing and doing some string parsing or something like that using Python,', 'start': 1069.68, 'duration': 7.266}, {'end': 1079.969, 'text': 'then you can pass in those results into a different command.', 'start': 1076.946, 'duration': 3.023}, {'end': 1081.19, 'text': "So that's definitely useful.", 'start': 1080.029, 'duration': 1.161}, {'end': 1083.871, 'text': "Okay, so I think that's going to do it for this video.", 'start': 1081.75, 'duration': 2.121}, {'end': 1093.797, 'text': 'I hope that you found it useful learning how you can call external commands using Python and also how to redirect those outputs and inputs and things like that.', 'start': 1084.192, 'duration': 9.605}], 'summary': 'Learn to call external commands and redirect inputs/outputs using python.', 'duration': 66.746, 'max_score': 1027.051, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y1027050.jpg'}], 'start': 548.289, 'title': 'Handling external command output and using python to call external commands', 'summary': 'Demonstrates redirecting command output to a file, capturing output in a variable, handling errors, including the non-zero error code returned by python, and redirecting errors to dev null. it also covers using python to call external commands, redirecting outputs and inputs, and processing them further, with examples of capturing output and changing input for different commands.', 'chapters': [{'end': 742.976, 'start': 548.289, 'title': 'Handling external command output in python', 'summary': 'Demonstrates redirecting command output to a file, capturing output in a variable, handling errors, including the non-zero error code returned by python, and redirecting errors to dev null.', 'duration': 194.687, 'highlights': ['Redirecting command output to a file by using subprocess in Python and accessing the output in the output.txt file', 'Capturing command output in a variable by setting the variable equal to capture_output and true, and discussing the consequences of a failed external command', 'Handling errors by checking the non-zero error code returned by Python and using conditional statements to proceed based on the success of the command', 'Throwing exceptions in Python by using the check=True argument to force Python to throw an exception when the external command fails']}, {'end': 1130.12, 'start': 743.517, 'title': 'Using python to call external commands', 'summary': 'Covers using python to call external commands, redirecting outputs and inputs, and processing them further, with examples of capturing output and changing input for different commands.', 'duration': 386.603, 'highlights': ['Using Python to call external commands and redirecting outputs and inputs The chapter covers using Python to call external commands, redirecting outputs and inputs, and processing them further.', 'Examples of capturing output and changing input for different commands The chapter provides examples of capturing output and changing input for different commands.', 'Using the output of one command as the input to another command The chapter demonstrates using the output of one command as the input to another command.', 'Demonstrating how to redirect errors to subprocess.devnull and capturing outputs The chapter demonstrates how to redirect errors to subprocess.devnull and capturing outputs.', 'Explaining the use of shell arguments and pipes in Linux for passing input The chapter explains the use of shell arguments and pipes in Linux for passing input.']}], 'duration': 581.831, 'thumbnail': 'https://coursnap.oss-ap-southeast-1.aliyuncs.com/video-capture/2Fp1N6dof0Y/pics/2Fp1N6dof0Y548289.jpg', 'highlights': ['Redirecting command output to a file by using subprocess in Python and accessing the output in the output.txt file', 'Using Python to call external commands and redirecting outputs and inputs', 'Capturing command output in a variable by setting the variable equal to capture_output and true, and discussing the consequences of a failed external command', 'Handling errors by checking the non-zero error code returned by Python and using conditional statements to proceed based on the success of the command', 'Using the output of one command as the input to another command', 'Demonstrating how to redirect errors to subprocess.devnull and capturing outputs', 'Throwing exceptions in Python by using the check=True argument to force Python to throw an exception when the external command fails', 'Examples of capturing output and changing input for different commands', 'Explaining the use of shell arguments and pipes in Linux for passing input']}], 'highlights': ['The subprocess module in Python allows for calling external commands and capturing their output, with the ability to run commands and pipe their output into another command.', "The 'ls' command in a Mac environment lists the files and folders in the current directory, while the equivalent in Windows is 'dir'.", "On Windows, using the 'dir' command may require passing an argument of shell equals true to prevent errors.", "Setting the shell argument to true allows passing an entire command as a string, and additional arguments like '-la' can be added to the 'ls' command for providing more information.", 'The need to pass arguments as a list When not using the shell argument, passing in the entire command as a string is not possible; instead, everything needs to be passed in as a list of arguments.', "The security hazard of using 'shell=true' with untrusted input Using 'shell=true' with untrusted input can be a security hazard, so it should only be used when passing in the arguments oneself and not with user input.", "Retrieving command outputs and checking return code The chapter explains how to capture standard output by setting 'capture_output=true' and checking the return code using 'returncode', with a return code of 0 indicating successful execution.", 'The subprocess.run command captures standard output in the variable, enabling it to be accessed and manipulated.', "Setting the 'capture_output' argument to true in subprocess.run redirects standard output and standard error to a subprocess pipe for capturing.", "Decoding bytes to string can be achieved by using the decode method or setting the 'text' argument to true.", 'Standard output can be redirected to a file by opening a file and redirecting the output to it.', 'Redirecting command output to a file by using subprocess in Python and accessing the output in the output.txt file', 'Using Python to call external commands and redirecting outputs and inputs', 'Capturing command output in a variable by setting the variable equal to capture_output and true, and discussing the consequences of a failed external command', 'Handling errors by checking the non-zero error code returned by Python and using conditional statements to proceed based on the success of the command', 'Using the output of one command as the input to another command', 'Demonstrating how to redirect errors to subprocess.devnull and capturing outputs', 'Throwing exceptions in Python by using the check=True argument to force Python to throw an exception when the external command fails', 'Examples of capturing output and changing input for different commands', 'Explaining the use of shell arguments and pipes in Linux for passing input']}