1 00:00:00,000 --> 00:00:00,800 [no audio] 2 00:00:00,800 --> 00:00:02,700 Friends, here we are going to discuss 3 00:00:02,700 --> 00:00:06,000 about 'os.system()' of your 'os' 4 00:00:06,000 --> 00:00:07,100 module. Actually, 5 00:00:07,100 --> 00:00:10,200 this 'os.system()' is a function because if you observe 6 00:00:10,200 --> 00:00:12,000 here, I have given parentheses. 7 00:00:12,000 --> 00:00:13,900 [no audio] 8 00:00:13,900 --> 00:00:15,500 The purpose of this 'os.system()' 9 00:00:15,500 --> 00:00:18,800 function from your 'os' module is just to execute 10 00:00:18,800 --> 00:00:20,700 your operating system commands. 11 00:00:22,000 --> 00:00:27,900 See as of now, we have seen some of the commands from your 12 00:00:27,900 --> 00:00:30,600 Python to work with your operating systems, 13 00:00:30,600 --> 00:00:34,400 like 'os.listdir()', 'removedir()', 'mkdir()', 14 00:00:34,400 --> 00:00:36,800 'chdir()', 'getcwd()', 15 00:00:36,900 --> 00:00:43,300 right? But in case if you don't have any command from your 16 00:00:43,300 --> 00:00:46,500 Python to work with your operating system, 17 00:00:47,500 --> 00:00:50,400 and if you know the command of your operating system, and 18 00:00:50,400 --> 00:00:53,200 if you want to execute your operating system command with 19 00:00:53,200 --> 00:00:56,800 Python, then this 'os.system()' is helpful. 20 00:00:57,800 --> 00:01:01,800 See there is a command called 'pwd', suppose on your Unix-like 21 00:01:01,800 --> 00:01:04,400 systems. What is the output you're getting? Current working 22 00:01:04,400 --> 00:01:09,000 directory or where you are on your terminal, right. See, 23 00:01:10,300 --> 00:01:13,300 with your 'os' module, right, 24 00:01:13,900 --> 00:01:14,900 'os.', 25 00:01:16,300 --> 00:01:19,700 'getcwd' is there, but assume that 26 00:01:19,700 --> 00:01:24,000 you forgot about this 'os.getcwd', because if you go 27 00:01:24,000 --> 00:01:28,800 with 'os.', suppose 'listdir()', there are different 28 00:01:28,800 --> 00:01:31,800 commands, right, from your Python to work with your operating 29 00:01:31,800 --> 00:01:34,000 system. But by mistake, 30 00:01:34,000 --> 00:01:36,000 [no audio] 31 00:01:36,000 --> 00:01:38,200 otherwise, simply assume that I forgot 32 00:01:38,200 --> 00:01:40,400 about this 'os.getcwd()', 33 00:01:40,400 --> 00:01:43,700 'os.listdir()', 'os.makedirs()', something like that. 34 00:01:44,700 --> 00:01:48,200 Then what I can do is, simply I can use 'os.system()', 35 00:01:48,200 --> 00:01:51,000 [no audio] 36 00:01:51,000 --> 00:01:54,800 and then I can run my particular operating system commands. See that. 37 00:01:54,800 --> 00:01:56,800 [no audio] 38 00:01:56,800 --> 00:01:58,800 And then 'os.system()', 39 00:01:59,100 --> 00:02:01,700 I can run 'ls' command. See the output. 40 00:02:01,700 --> 00:02:04,600 [no audio] 41 00:02:04,600 --> 00:02:08,500 Right. You're able to execute your operating system commands 42 00:02:08,500 --> 00:02:10,100 with the help of 'os.system()'. 43 00:02:10,699 --> 00:02:12,500 Not only Linux, 44 00:02:12,500 --> 00:02:15,700 you can also execute from your Windows as well. 45 00:02:15,700 --> 00:02:17,200 Let me enter into my Python. 46 00:02:17,700 --> 00:02:20,800 Let me 'import os'. 'os.system()'. 47 00:02:20,800 --> 00:02:24,000 [no audio] 48 00:02:24,000 --> 00:02:26,500 I can run something like 'dir' command. 49 00:02:26,900 --> 00:02:28,000 Yes, you are getting output. 50 00:02:29,000 --> 00:02:30,300 Right. 'dir' is the command for 51 00:02:30,300 --> 00:02:32,800 your Windows operating system to list your directories and 52 00:02:32,800 --> 00:02:34,100 files in the current location. 53 00:02:34,500 --> 00:02:38,000 Same thing you can run with your 'os.system()' as well through 54 00:02:38,000 --> 00:02:42,300 Python. So simply 'os.system()' is helpful to execute 55 00:02:42,300 --> 00:02:44,800 your operating system commands. 56 00:02:46,000 --> 00:02:48,600 See, to clear my terminal, right, 57 00:02:48,600 --> 00:02:53,300 if it is Linux I'm using 'clear' command, if it is Windows 58 00:02:53,300 --> 00:02:55,800 I have to use 'cls', right. See that what I am doing, 59 00:02:56,800 --> 00:03:00,200 'os.system()'. Because I am on Linux as of now 60 00:03:00,200 --> 00:03:02,200 I am running 'os.system("clear")'. 61 00:03:02,400 --> 00:03:04,000 Now, your terminal is clearing. 62 00:03:04,500 --> 00:03:07,500 Let me open your Windows terminal. Now, 63 00:03:07,500 --> 00:03:11,200 I am in Python shell but I want to execute Windows command 64 00:03:11,200 --> 00:03:13,200 called 'cls'. Yes, you can execute it. 65 00:03:13,400 --> 00:03:15,000 You know, what is the result of 'cls'? 66 00:03:15,100 --> 00:03:16,700 It will simply clear your screen. 67 00:03:17,200 --> 00:03:20,300 Yes, that's what it is doing. 68 00:03:21,100 --> 00:03:24,100 But if you observe here guys, while running your commands, 69 00:03:24,100 --> 00:03:28,000 it may be any command, right, see the output. You are getting, 70 00:03:28,000 --> 00:03:30,200 you are able to display your output directly with the help 71 00:03:30,200 --> 00:03:34,600 of your 'os.system()', right, at the same time you are getting 72 00:03:34,600 --> 00:03:35,900 some return code. 73 00:03:36,800 --> 00:03:40,300 So here you are getting 0, that means whatever the command 74 00:03:40,300 --> 00:03:42,000 you are running with 'os.system()', 75 00:03:42,300 --> 00:03:43,400 that is success. 76 00:03:44,800 --> 00:03:46,700 Right. Not only there, here also 77 00:03:46,700 --> 00:03:50,400 let me simply run 'os.system()'. 78 00:03:51,100 --> 00:03:52,700 Now, I am on Unix box. 79 00:03:52,700 --> 00:03:56,200 I am running 'pwd' command. See the result. You are getting path, 80 00:03:56,300 --> 00:03:59,100 current working directory, and also you're getting some 0. That is 81 00:03:59,100 --> 00:04:01,200 the return code from your 'os.system()'. 82 00:04:01,600 --> 00:04:04,800 If it is 0 then whatever the command you are running, that 83 00:04:04,800 --> 00:04:08,800 was success, that was successfully executed by your 'os.system()'. 84 00:04:09,100 --> 00:04:12,200 Let's say I am running 'os.system()', something called 'random' 85 00:04:12,200 --> 00:04:14,500 command, which is not a valid command 86 00:04:14,600 --> 00:04:16,100 as per your operating system, 87 00:04:16,200 --> 00:04:18,399 that's why, see the output what you are getting. You are 88 00:04:18,399 --> 00:04:21,700 getting some error, along with that return code as non-zero. 89 00:04:23,399 --> 00:04:29,800 See that. I am writing something like that, 'rt = 'os.system()', 90 00:04:31,400 --> 00:04:37,100 some 'ls'. Guys if I run in this way, be clear, it's very important, 91 00:04:37,700 --> 00:04:41,800 whatever the output you are going to get from your 'os.system()' 92 00:04:41,800 --> 00:04:45,600 with the help of, to run your 'ls' command, that 93 00:04:45,600 --> 00:04:50,000 output won't go and store into this. Simply the return status 94 00:04:50,000 --> 00:04:55,400 of this executed syntax, that will store into this 'rt'. That 95 00:04:55,400 --> 00:05:00,100 means, in case if it is executing successfully then 0 will 96 00:05:00,100 --> 00:05:02,800 store into this, otherwise non-zero will store into that. Now, 97 00:05:02,800 --> 00:05:05,900 see the output. So previously whenever if you're running your 98 00:05:05,900 --> 00:05:08,300 command you are getting output and also some return code, 99 00:05:08,400 --> 00:05:12,400 but now I am assigning that return code into some variable. 100 00:05:12,400 --> 00:05:13,500 Now you're getting only output. 101 00:05:13,500 --> 00:05:15,500 [no audio] 102 00:05:15,500 --> 00:05:18,300 Make sure that 'os.system()' 103 00:05:18,300 --> 00:05:21,100 output you're not able to store into any variable. 104 00:05:21,100 --> 00:05:23,900 You can't store. The only thing, you can directly display your 105 00:05:23,900 --> 00:05:28,600 output. But what it will store? In case if you assign your 106 00:05:28,600 --> 00:05:32,200 'os.system()' some command output into a variable, 107 00:05:32,700 --> 00:05:35,300 what is the value you are going to store into that? Simply, 108 00:05:36,900 --> 00:05:41,800 simply the executed command status, whether 109 00:05:41,800 --> 00:05:44,800 it is success or fail, that status you're going to store into 110 00:05:44,800 --> 00:05:48,200 'rt'. Right. See that. Let me write a simple script 111 00:05:48,200 --> 00:05:49,200 so that you can understand. 112 00:05:50,500 --> 00:05:56,700 'vi run_os_cmds'. I am writing 'run_os.py', or 113 00:05:56,700 --> 00:05:59,900 'os_cmds' instead of this. Just for clarity 114 00:05:59,900 --> 00:06:04,600 I'm writing 'run_os_cmds.py', or any command also. 115 00:06:05,100 --> 00:06:07,100 So I can run with the help of 'os', 116 00:06:07,600 --> 00:06:08,900 I mean 'os.system()'. 117 00:06:09,100 --> 00:06:10,800 You're also having different procedures guys, 118 00:06:10,800 --> 00:06:13,600 we will see while going forward. For time being 119 00:06:13,600 --> 00:06:15,400 it is important, I am working with this. 120 00:06:17,000 --> 00:06:18,500 See, command is always a string. 121 00:06:18,600 --> 00:06:21,700 You have to remember that. Suppose my command is, let's say 122 00:06:21,700 --> 00:06:22,700 date command. 123 00:06:24,000 --> 00:06:28,100 Right. Now what I am doing is, 'os.system()', then your command. 124 00:06:28,200 --> 00:06:31,900 So already I stored my command into 'cmd' variable as a string, 125 00:06:31,900 --> 00:06:33,000 now I can provide that. 126 00:06:34,100 --> 00:06:38,100 Let me store it and see your script once. Now, 127 00:06:38,100 --> 00:06:42,100 I am going to run this script. You're getting output. 128 00:06:42,100 --> 00:06:44,500 [no audio] 129 00:06:44,500 --> 00:06:48,200 Right. Whenever if you're running your 'os.system()' from 130 00:06:48,200 --> 00:06:51,400 your Python script, no need to print the result. Automatically 131 00:06:51,400 --> 00:06:52,700 it is printing the result. 132 00:06:54,100 --> 00:06:59,900 Right. Now what I am doing is, maybe as of now date command is perfect 133 00:06:59,900 --> 00:07:01,500 that's why we are getting output, right? 134 00:07:01,500 --> 00:07:05,200 Maybe I am taking some wrong command. Now see the output. 135 00:07:05,200 --> 00:07:09,700 [no audio] 136 00:07:09,700 --> 00:07:12,200 Now whenever if it is wrong command your 'os.system()' 137 00:07:12,200 --> 00:07:14,000 will give some return codes, 138 00:07:14,000 --> 00:07:15,100 I want to capture that. 139 00:07:15,400 --> 00:07:20,100 Let me take rt = 'os.system()', some command. Now, 140 00:07:20,100 --> 00:07:24,900 I can take some 'if' condition, 'if rt == 0', then I can say that, 141 00:07:25,390 --> 00:07:26,300 'print()', 142 00:07:27,900 --> 00:07:31,500 "your", whatever the command you're having, "your cmd was 143 00:07:32,300 --> 00:07:34,100 success", "successfully executed". 144 00:07:34,100 --> 00:07:39,200 [no audio] 145 00:07:39,200 --> 00:07:40,200 'else', 146 00:07:40,200 --> 00:07:43,000 [no audio] 147 00:07:43,000 --> 00:07:47,500 'else' because if 'rt' is not equal to 0 means fail, "your 148 00:07:47,500 --> 00:07:48,700 command was fail". 149 00:07:48,700 --> 00:07:52,200 [no audio] 150 00:07:52,200 --> 00:07:54,000 That's it. Very simple no. 151 00:07:55,000 --> 00:07:59,000 I can capture the return status of this command means if 152 00:07:59,000 --> 00:08:01,400 this command was success then I can capture 153 00:08:02,800 --> 00:08:05,800 0 into this 'rt' variable, you can take any variable 154 00:08:05,800 --> 00:08:07,700 here, not only 'rt'. Simply I'm taking return code, 155 00:08:07,700 --> 00:08:10,000 that's why I am taking 'rt' as the variable, but you can 156 00:08:10,000 --> 00:08:11,200 take simply 'x' also. 157 00:08:12,800 --> 00:08:14,300 Right. Now see the output. 158 00:08:14,900 --> 00:08:16,900 I'm running your script. Before running 159 00:08:16,900 --> 00:08:19,400 just open your script, right. 160 00:08:19,500 --> 00:08:22,800 See this is your script, and now I am running that. See the 161 00:08:22,800 --> 00:08:24,600 result, "your command was failed". 162 00:08:24,600 --> 00:08:27,200 [no audio] 163 00:08:27,200 --> 00:08:29,700 Right. Now see that. What I am doing is, 164 00:08:29,700 --> 00:08:33,200 [no audio] 165 00:08:33,200 --> 00:08:34,600 "Your command", right? 166 00:08:34,600 --> 00:08:38,299 Yeah, I'm going to give now a valid command. Date is 167 00:08:38,200 --> 00:08:39,400 a valid command, right? 168 00:08:39,400 --> 00:08:41,900 [no audio] 169 00:08:41,900 --> 00:08:45,700 Now see that. "your cmd was successfully executed". 170 00:08:46,900 --> 00:08:52,500 So guys make sure that 'os.system()' is used to execute your 171 00:08:52,600 --> 00:08:57,400 any operating system commands and while executing your commands 172 00:08:57,600 --> 00:09:01,400 with 'os.system()' in your Python script by default 173 00:09:01,400 --> 00:09:05,100 it will print the output of your command on your terminal 174 00:09:05,700 --> 00:09:06,900 while running your script. 175 00:09:08,000 --> 00:09:11,000 At the same time it is also going to give 176 00:09:11,000 --> 00:09:14,700 some return code based on whether your command is success or not. 177 00:09:14,700 --> 00:09:18,300 If your command was success 'os.system()' will 178 00:09:18,300 --> 00:09:22,700 give 0 as the code, if your command was failed then your 179 00:09:22,700 --> 00:09:27,700 'os.system()' command will give return code or code 180 00:09:27,700 --> 00:09:30,500 as non-zero value. Based on that I can decide whether it is 181 00:09:30,500 --> 00:09:31,400 success or not. 182 00:09:31,400 --> 00:09:33,900 [no audio] 183 00:09:33,900 --> 00:09:35,700 Right. Okay. 184 00:09:36,500 --> 00:09:39,300 Okay guys, so while going forward we will use this 'os.system()' 185 00:09:39,300 --> 00:09:41,000 command, and we will write lot of scripts using this 186 00:09:41,000 --> 00:09:43,700 'os.system()', right? 187 00:09:44,100 --> 00:09:46,100 Okay, thank you for watching this video. 188 00:09:46,100 --> 00:09:53,700 [no audio]