1 00:00:00,000 --> 00:00:02,500 [no audio] 2 00:00:02,500 --> 00:00:04,000 Friends, here we are going to write 3 00:00:04,000 --> 00:00:06,300 one simple Python script. The purpose 4 00:00:06,300 --> 00:00:10,700 of your Python script is find all files in a directory with 5 00:00:10,700 --> 00:00:15,400 extension - '.py', or '.sh', or maybe any your required 6 00:00:15,400 --> 00:00:18,900 extension. I mean while running your Python script first 7 00:00:18,900 --> 00:00:21,600 of all it has to ask, "Enter your directory path". 8 00:00:22,100 --> 00:00:23,600 After giving your path, 9 00:00:24,100 --> 00:00:28,900 it has to ask, "Enter the extension of your required files". 10 00:00:29,400 --> 00:00:32,400 Suppose let's say I am giving '.py' extension, 11 00:00:32,400 --> 00:00:36,100 then it has to display only '.py' files in the given location. 12 00:00:36,800 --> 00:00:40,500 Suppose if I give '.sh', then it has to display only 13 00:00:41,000 --> 00:00:43,300 '.ssh' files in the given location. 14 00:00:44,800 --> 00:00:46,900 Right. Let me go and write my script. 15 00:00:47,000 --> 00:00:49,800 So as of now, I am going to open my 16 00:00:51,000 --> 00:00:54,900 Unix box. Same script will run even on your Windows as well. 17 00:00:55,500 --> 00:00:56,800 Right. See that. 18 00:00:57,900 --> 00:00:59,600 Let me take some simple script as 19 00:00:59,600 --> 00:01:07,099 'get_required_extension_files.py'. 20 00:01:07,900 --> 00:01:09,200 See this is shebang. Sorry, 21 00:01:09,200 --> 00:01:10,900 this is Unix-like system, 22 00:01:10,900 --> 00:01:12,800 you can also write your shebang line. 23 00:01:13,400 --> 00:01:18,300 So, this is my Python location, but don't write this whenever 24 00:01:18,300 --> 00:01:22,200 if you are running your script on your Windows systems. It 25 00:01:22,200 --> 00:01:25,600 will work only on your Unix-like systems. 26 00:01:26,600 --> 00:01:30,900 Okay. Now the first thing is our script has to read a path. Path 27 00:01:30,900 --> 00:01:34,400 is always a string. We know in Python 3 to read your path 28 00:01:34,400 --> 00:01:38,800 we have, let me take 'usr_path' or 'req_path'. 29 00:01:38,800 --> 00:01:45,700 I am taking 'req_path', right. 'input(:Enter your path: ")' 30 00:01:46,200 --> 00:01:49,700 "Enter your directory path:", 31 00:01:51,100 --> 00:01:52,100 That's it. 32 00:01:53,100 --> 00:01:57,900 Then, after giving your directory path immediately you have to 33 00:01:57,900 --> 00:02:02,700 ask the user, required extension. Now in that given location, 34 00:02:03,300 --> 00:02:07,100 what are the files you need, with what extension you need, 35 00:02:07,300 --> 00:02:15,200 right. That's why, "Enter the required files extension: ". 36 00:02:15,200 --> 00:02:17,500 [no audio] 37 00:02:17,500 --> 00:02:23,900 Example, I'm giving '.py', or '.sh', '.log', maybe anything 38 00:02:23,900 --> 00:02:25,000 based on user requirement, 39 00:02:25,000 --> 00:02:26,800 maybe '.txt', whatever it may be. 40 00:02:27,400 --> 00:02:29,800 That's what I want to read from the user. 41 00:02:31,200 --> 00:02:37,000 Fine. So first of all user is, okay, as of now you are going to 42 00:02:37,000 --> 00:02:40,600 run this script. See that, how it is going to work. 43 00:02:40,600 --> 00:02:43,400 Let me take some path, this as my path suppose. 44 00:02:44,900 --> 00:02:46,400 Let me come out from here. 45 00:02:46,600 --> 00:02:49,700 Then I'm going to run our Python script. Before going to run, 46 00:02:50,000 --> 00:02:53,100 let me provide execution permissions. Now because this is 47 00:02:53,100 --> 00:02:56,300 Linux system, after providing your permissions, after giving 48 00:02:56,300 --> 00:02:57,600 your shebang line, 49 00:02:57,600 --> 00:02:59,800 you can run your Python script with './'. 50 00:03:00,500 --> 00:03:02,400 So it is asking, "Enter your directory path: ". 51 00:03:02,500 --> 00:03:05,500 Now, I'm entering suppose this one, any path, 52 00:03:05,600 --> 00:03:08,100 your required both path you can give. After that 53 00:03:08,100 --> 00:03:12,200 it is asking, "Enter your required extension for your 54 00:03:12,200 --> 00:03:16,700 files: ". I'm entering suppose '.py'. Your script is ending, 55 00:03:16,700 --> 00:03:20,100 because as of now we don't have any logic to find out your 56 00:03:20,100 --> 00:03:22,600 extensions, your files with extension. 57 00:03:23,800 --> 00:03:29,400 Only we have a logic to read path and then to read your extension 58 00:03:29,400 --> 00:03:31,400 of a file. Fine. 59 00:03:32,200 --> 00:03:37,200 See suppose given path, by mistake suppose if user 60 00:03:37,200 --> 00:03:39,000 enters a file path. 61 00:03:40,100 --> 00:03:42,800 Right. See suppose if I take this as a path, this is directory 62 00:03:42,800 --> 00:03:43,800 path suppose. 63 00:03:44,300 --> 00:03:48,800 Now if I take suppose something like, we don't have anything 64 00:03:48,800 --> 00:03:53,500 here. Yeah, 'get_req', this, if I give this one, right, this is 65 00:03:53,500 --> 00:03:56,900 not a directory. If it is not a directory you don't have anything inside of that. 66 00:03:56,900 --> 00:03:59,100 [no audio] 67 00:03:59,100 --> 00:04:02,100 So that's why this path is always a directory. 68 00:04:02,400 --> 00:04:05,600 So what I want to do is, after reading your directory path, 69 00:04:05,600 --> 00:04:08,700 I'm checking whether this is a valid directory path or not. 70 00:04:09,600 --> 00:04:12,300 So it's a good practice actually to write that logic. 71 00:04:12,700 --> 00:04:15,900 So before reading your extension, right, I can also write 72 00:04:15,900 --> 00:04:18,899 my logic after reading extension, but before reading extension 73 00:04:19,399 --> 00:04:25,200 I am writing my logic. That is, 'if' given path is file or directory 74 00:04:25,200 --> 00:04:28,200 I want to check it. You know in your Python you have a 75 00:04:28,200 --> 00:04:30,300 'os' module. In that 'os' module 76 00:04:30,300 --> 00:04:35,400 we know we have 'os.path.isfile' or 'isdir'. 77 00:04:36,000 --> 00:04:42,000 So now what I am checking is 'os.path.isfile', if it is a file 78 00:04:42,000 --> 00:04:45,600 I want to stop my script and then I want to say to the user, 79 00:04:47,100 --> 00:04:48,800 "Please pass only directory path". 80 00:04:49,700 --> 00:04:53,300 Okay. So guys this script I am implementing just to give 81 00:04:53,400 --> 00:04:57,200 idea about your modules and some concepts. Okay. 82 00:04:57,400 --> 00:04:59,300 You may write the same code in a different way. 83 00:04:59,400 --> 00:05:01,400 No problem. Fine. 84 00:05:01,400 --> 00:05:05,300 Now, what I am doing is, in case if the given path is a file, 85 00:05:05,300 --> 00:05:07,600 then what I want to say to the user? 'print( 86 00:05:07,600 --> 00:05:10,400 [no audio] 87 00:05:10,400 --> 00:05:13,900 "The given path", whatever the user is going to give, "The given 88 00:05:13,900 --> 00:05:15,900 path", that path, 89 00:05:17,300 --> 00:05:20,400 is a file. 90 00:05:20,400 --> 00:05:22,300 [no audio] 91 00:05:22,300 --> 00:05:28,600 Please pass only directory path")'. That's it. 92 00:05:28,600 --> 00:05:31,300 [no audio] 93 00:05:31,300 --> 00:05:35,900 I am closing. Then, 'else', 'else', right. 94 00:05:36,000 --> 00:05:37,600 So here I need to write my logic. 95 00:05:38,200 --> 00:05:42,400 So for time being I will write this also, so that you will 96 00:05:42,400 --> 00:05:43,400 get some ideas. 97 00:05:44,800 --> 00:05:45,900 I am writing here. Now, 98 00:05:45,900 --> 00:05:47,400 let me remove this path here. 99 00:05:48,200 --> 00:05:50,100 So here I need to implement my logic. As of now 100 00:05:50,100 --> 00:05:51,100 I'm not implementing. 101 00:05:51,100 --> 00:05:52,800 We'll come back and we'll implement. 102 00:05:52,800 --> 00:05:59,900 Okay. So, "implement your actual logic here". We'll implement it 103 00:05:59,900 --> 00:06:04,100 after some time. Just wait. Now, let me save it. Right. 104 00:06:04,200 --> 00:06:08,300 So what I am doing is, let me do one thing. Under 'Udemy' 105 00:06:08,300 --> 00:06:09,300 let me take some, 106 00:06:10,600 --> 00:06:11,600 'read'. 107 00:06:11,600 --> 00:06:16,600 [no audio] 108 00:06:16,600 --> 00:06:17,600 I will do one thing. 109 00:06:17,600 --> 00:06:22,600 [no audio] 110 00:06:22,600 --> 00:06:24,200 Okay, come back. 111 00:06:24,400 --> 00:06:26,500 And this is your script, right. Now, 112 00:06:26,500 --> 00:06:30,100 I'm running that. So it is asking, "Enter your directory path: ". 113 00:06:30,100 --> 00:06:33,500 But suppose, just assume that I am going to give a path called, 114 00:06:33,500 --> 00:06:35,800 [no audio] 115 00:06:35,800 --> 00:06:39,600 in this location I am going to give some files path. 116 00:06:40,900 --> 00:06:45,500 Let's say, it may be anything, then script is saying that, see 117 00:06:45,500 --> 00:06:47,100 that. "Enter required", 118 00:06:47,100 --> 00:06:48,400 okay, "required files extension' 119 00:06:48,400 --> 00:06:54,000 Let me enter '.py'. "The given path is a file. Please pass 120 00:06:54,000 --> 00:06:58,900 only directory path." Suppose if I enter, if I enter only directory 121 00:06:58,900 --> 00:07:01,300 path. Let me take, 122 00:07:01,300 --> 00:07:03,300 [no audio] 123 00:07:03,300 --> 00:07:04,300 this one. 124 00:07:05,600 --> 00:07:06,700 Now your script will display 125 00:07:06,700 --> 00:07:09,600 [no audio] 126 00:07:09,600 --> 00:07:10,600 "implement your actual logic", 127 00:07:10,600 --> 00:07:11,900 we need implement that part. 128 00:07:13,400 --> 00:07:16,300 Right. Now see, one more thing. 129 00:07:16,300 --> 00:07:18,500 [no audio] 130 00:07:18,500 --> 00:07:20,400 Here we are going to implement our logic. 131 00:07:20,400 --> 00:07:22,300 [no audio] 132 00:07:22,300 --> 00:07:26,300 Okay. First of all what I will do is, I will list all your directories 133 00:07:26,300 --> 00:07:30,100 and files in the given location. See in 'else' block only, because 134 00:07:30,100 --> 00:07:32,400 in 'if' condition what we are checking? If it is a file then 135 00:07:32,400 --> 00:07:35,400 we are displaying this information. You know, if this is 136 00:07:35,400 --> 00:07:37,100 'True', then your 'else' block won't execute. 137 00:07:37,100 --> 00:07:40,400 If this is 'False' means you are giving valid directory path. 138 00:07:41,500 --> 00:07:42,900 Then you are going to execute this one. 139 00:07:44,500 --> 00:07:46,700 Right. Now see what I am doing is, 140 00:07:46,700 --> 00:07:49,000 [no audio] 141 00:07:49,000 --> 00:07:51,200 'print'. See in the given location 142 00:07:51,200 --> 00:07:53,700 if you want to list all your directories and files, 143 00:07:55,400 --> 00:08:00,300 if you remember, there is a operation called 'os.listdir()', 144 00:08:01,300 --> 00:08:02,700 then your required path. 145 00:08:02,700 --> 00:08:05,200 [no audio] 146 00:08:05,200 --> 00:08:07,800 Okay, let me save it, and run it, and see the result. 147 00:08:08,500 --> 00:08:11,400 I'm running, and I'm going to provide your path as 148 00:08:11,400 --> 00:08:14,800 [no audio] 149 00:08:14,800 --> 00:08:19,900 directory path. Okay. Let me provide extension '.py'. Yes, in the given 150 00:08:19,900 --> 00:08:22,700 location you are getting all the files which are there, 151 00:08:22,700 --> 00:08:26,700 but actually our intention is, we have to display only 152 00:08:26,700 --> 00:08:29,500 suppose '.py' or '.sh', whatever it may be, whatever the 153 00:08:29,500 --> 00:08:34,100 user is going to enter, that extension files we need to identify. 154 00:08:35,200 --> 00:08:38,000 So now what I have to do? I have to take one by one file 155 00:08:38,000 --> 00:08:40,799 from here and I need to check it whether it is ending with '.py' 156 00:08:40,799 --> 00:08:45,299 or not. You know how to take one by one file from a list, 157 00:08:45,700 --> 00:08:47,299 by using for loop you can take it. 158 00:08:48,400 --> 00:08:51,500 But before that, let me tell you one more thing. See whatever 159 00:08:51,500 --> 00:08:54,799 the path you are going to enter, if it is empty then why should 160 00:08:54,799 --> 00:08:57,800 we check whether file is ending with '.py' or not? 161 00:08:59,100 --> 00:09:03,600 Right. So that's why what I am doing is, before writing your actual 162 00:09:03,600 --> 00:09:04,600 logic I am checking, 163 00:09:05,400 --> 00:09:06,300 I am checking. First 164 00:09:06,300 --> 00:09:12,700 let me find out all files and directories, all files and directories 165 00:09:12,800 --> 00:09:13,800 in the given location. 166 00:09:14,400 --> 00:09:16,800 Then after that I am writing one more 'if' condition inside 167 00:09:16,800 --> 00:09:23,100 of your 'else'. That is, 'if len()', of, you know what is the 'len()' 168 00:09:23,100 --> 00:09:25,700 function. It is going to find out number of values in the 169 00:09:25,700 --> 00:09:29,700 given list. You know this operation output is a list guys. Now 170 00:09:29,700 --> 00:09:33,000 I'm checking how many values are there in this. By mistake 171 00:09:33,700 --> 00:09:36,700 if you have 0 values means there are no files and directories 172 00:09:36,700 --> 00:09:37,800 in the given location. 173 00:09:38,200 --> 00:09:40,700 So at that time I don't need to find any logic, right. So I 174 00:09:40,700 --> 00:09:42,400 am writing simply, 'print( 175 00:09:42,400 --> 00:09:44,700 [no audio] 176 00:09:44,700 --> 00:09:57,200 "The given path is an empty path")', so that path I am writing here. 177 00:09:57,800 --> 00:10:00,700 What is the given path? 'req_path'. That's it. 178 00:10:00,700 --> 00:10:04,500 [no audio] 179 00:10:04,500 --> 00:10:07,700 Let me check it whether it is working perfectly or not. 'else' 180 00:10:07,800 --> 00:10:10,000 'else', then we have to write actual logic. 181 00:10:10,300 --> 00:10:15,600 So then, 'else:', here I am going to write 'print("implement your 182 00:10:15,600 --> 00:10:19,000 logic")'. So as of now we are not implementing. Yes, 183 00:10:19,000 --> 00:10:21,700 we will implement it, right. Just wait. 184 00:10:22,800 --> 00:10:25,300 Now there is a directory called 'empty'. 185 00:10:25,400 --> 00:10:30,400 I am taking this 'empty' directory, and then let me take this 186 00:10:30,400 --> 00:10:35,000 one, and also that path, then come back, then we have our file 187 00:10:35,000 --> 00:10:36,900 here. Let me run it. 188 00:10:37,300 --> 00:10:40,700 Oh man, one extra parentheses. 189 00:10:41,500 --> 00:10:42,600 Let me remove that. 190 00:10:42,600 --> 00:10:44,600 [no audio] 191 00:10:44,600 --> 00:10:47,000 See that directory path 192 00:10:47,000 --> 00:10:52,300 I'm going to in this remove this parentheses. That's it. 193 00:10:53,700 --> 00:10:55,500 Right. Fine. Now, 194 00:10:55,500 --> 00:10:59,300 I am running this one. See the output. "Enter your directory 195 00:10:59,300 --> 00:11:03,300 path: ". I am entering suppose "Udemy" path. Now see the output. 196 00:11:04,300 --> 00:11:06,400 Okay, "Enter the required files extension", as well. 197 00:11:06,400 --> 00:11:09,700 Let me enter it. "implement your logic". Actually, 198 00:11:09,700 --> 00:11:11,600 this is not an empty directory, right? 199 00:11:12,600 --> 00:11:13,600 That's why 200 00:11:14,400 --> 00:11:17,700 "implement your logic". Now what I am doing is, 201 00:11:19,400 --> 00:11:22,000 I am going to enter 'empty' directory path now. 202 00:11:22,000 --> 00:11:24,600 [no audio] 203 00:11:24,600 --> 00:11:28,500 Now see the result. "The given path is", whatever the path, "an 204 00:11:28,500 --> 00:11:29,600 empty path". 205 00:11:29,600 --> 00:11:31,600 [no audio] 206 00:11:31,600 --> 00:11:34,700 Right. That's why no need to find '.py' files. 207 00:11:35,700 --> 00:11:39,000 But if you observe your logic clearly, let me open it first. 208 00:11:39,000 --> 00:11:41,800 [no audio] 209 00:11:41,800 --> 00:11:45,600 Before checking whether the given path is empty or not, 210 00:11:46,000 --> 00:11:48,600 you're also reading required file extension. 211 00:11:49,400 --> 00:11:53,000 Now in case if your given path is an empty path, 212 00:11:53,100 --> 00:11:54,700 why should we read this extension guys? 213 00:11:55,600 --> 00:11:56,700 That's why now 214 00:11:56,700 --> 00:12:00,000 what I am doing is, I am going to comment that line, or I am 215 00:12:00,000 --> 00:12:02,900 going to remove that line there and I want to write somewhere 216 00:12:02,900 --> 00:12:06,700 else. Where I have to write? If your given path is having 217 00:12:06,700 --> 00:12:09,300 some files then only I want to read extension. 218 00:12:09,300 --> 00:12:11,100 [no audio] 219 00:12:11,100 --> 00:12:14,500 Right. See if 'else' block is executing means, 220 00:12:14,500 --> 00:12:16,600 [no audio] 221 00:12:16,600 --> 00:12:17,800 inside of your 'else' 222 00:12:18,000 --> 00:12:20,900 you have one more 'if-else'. If this 'else' is executing means 223 00:12:20,900 --> 00:12:24,400 you have some list of files in the given location, then only 224 00:12:24,400 --> 00:12:25,700 I want to read an extension. 225 00:12:25,700 --> 00:12:28,600 [no audio] 226 00:12:28,600 --> 00:12:33,900 Right. Now see, let me save it, and run it, and before going 227 00:12:33,900 --> 00:12:36,900 to run let me take my path, this one. 228 00:12:38,000 --> 00:12:40,700 Now, I am providing 'empty' path. Now directly 229 00:12:40,700 --> 00:12:43,700 it is giving, "Given path is an empty path". 230 00:12:45,000 --> 00:12:49,600 Now, if I give you some valid, I mean path which is having 231 00:12:49,600 --> 00:12:51,400 some values, now 232 00:12:51,400 --> 00:12:56,300 it will also ask, "Enter your extension", right. If you give empty 233 00:12:56,300 --> 00:13:01,400 path it is not asking. If you give some path which is having 234 00:13:01,400 --> 00:13:05,100 some values, some files and directories, then only it is asking 235 00:13:05,100 --> 00:13:06,200 "Enter your extension: ". 236 00:13:06,300 --> 00:13:07,100 That's good actually. 237 00:13:07,900 --> 00:13:10,300 So unnecessarily we are not going to read extension if your 238 00:13:10,300 --> 00:13:11,400 given path is an empty. 239 00:13:12,600 --> 00:13:19,000 Fine. Now what I am doing is, after reading your extension our 240 00:13:19,000 --> 00:13:24,700 requirement is, we have to take one by one file from all your 241 00:13:24,700 --> 00:13:25,800 files and directories. 242 00:13:27,000 --> 00:13:30,000 See I am taking 'for each_f in', your 243 00:13:31,400 --> 00:13:32,400 list of files 244 00:13:32,400 --> 00:13:34,800 [no audio] 245 00:13:34,800 --> 00:13:36,000 Let me first 'print 246 00:13:36,000 --> 00:13:37,900 [no audio] 247 00:13:37,900 --> 00:13:40,600 (each_f)', in the given location")'. 248 00:13:40,600 --> 00:13:42,400 [no audio] 249 00:13:42,500 --> 00:13:44,400 Right. Let me run it. 250 00:13:44,400 --> 00:13:46,300 [no audio] 251 00:13:46,300 --> 00:13:50,100 './', I am going to run. It is asking, "Enter your path: ". 252 00:13:50,200 --> 00:13:52,800 Let me enter my path as suppose this one. 253 00:13:54,100 --> 00:13:58,300 Yes. Now I am giving extension as '.py', but your script is 254 00:13:58,300 --> 00:14:01,200 printing all the files, right, files and directories. 255 00:14:02,200 --> 00:14:06,100 I don't want to print all. The files which are having, which 256 00:14:06,100 --> 00:14:08,600 are ending with '.py', those files only 257 00:14:08,600 --> 00:14:12,500 I want to print. That's why again in your 'if' condition, sorry, 258 00:14:12,500 --> 00:14:15,500 again in your for loop you have to add a 'if' condition before 259 00:14:15,500 --> 00:14:20,200 printing your files. See what I am doing. Before printing your 260 00:14:20,300 --> 00:14:26,800 file 'if', if you remember string operation is there, that is 261 00:14:26,800 --> 00:14:30,400 'endswith', your path is always, 262 00:14:31,900 --> 00:14:35,900 your path is always string. Now file is a string. 263 00:14:36,000 --> 00:14:39,900 Directory is a string. Now simply I am doing, is it ending with 264 00:14:40,900 --> 00:14:42,000 '.py' extension? 265 00:14:43,600 --> 00:14:45,900 If it is really ending with '.py' extension, then only 266 00:14:45,900 --> 00:14:49,600 I want to print, otherwise I don't bother about that, right. 267 00:14:50,300 --> 00:14:53,700 Now, let me run your script and see the result. Now 268 00:14:53,700 --> 00:14:58,500 I am giving path as, and I am giving '.py'. See the output. 269 00:14:58,500 --> 00:15:01,600 You're getting only '.py' files. Now, 270 00:15:01,600 --> 00:15:06,600 let me run your script once again, and let me provide extension 271 00:15:06,600 --> 00:15:09,500 as suppose '.sh'. See the output. 272 00:15:09,900 --> 00:15:12,600 [no audio] 273 00:15:12,600 --> 00:15:17,100 The problem is directly I fixed, extension is '.py', but 274 00:15:17,100 --> 00:15:20,500 what about the path extension you are reading, that variable 275 00:15:20,500 --> 00:15:21,500 you have to write now. 276 00:15:22,900 --> 00:15:26,000 So actually this, in this you are storing your extension, right? 277 00:15:27,200 --> 00:15:28,200 Now it will work. 278 00:15:28,200 --> 00:15:30,700 [no audio] 279 00:15:30,700 --> 00:15:32,400 Let me enter your path first. 280 00:15:32,400 --> 00:15:34,700 [no audio] 281 00:15:34,700 --> 00:15:36,100 Let me enter '.sh'. 282 00:15:36,800 --> 00:15:39,200 Yes, you are getting only '.ssh' files. 283 00:15:40,900 --> 00:15:45,400 Let me rerun and provide suppose '.py'. Now you're getting 284 00:15:45,400 --> 00:15:46,600 only '.py' files. 285 00:15:47,600 --> 00:15:48,800 Right. Fine. 286 00:15:49,800 --> 00:15:53,400 And if you observe your script, your script is displaying 287 00:15:53,400 --> 00:15:58,400 individually your required files, but I don't want to print 288 00:15:58,400 --> 00:16:03,900 individually. I want to print all your '.py', '.sh' as a list. 289 00:16:03,900 --> 00:16:05,600 [no audio] 290 00:16:05,600 --> 00:16:06,700 Then how you can do it? 291 00:16:07,600 --> 00:16:10,700 Right. See guys whenever if you want to create a list based on 292 00:16:10,700 --> 00:16:14,200 your required values, first of all you have to define somewhere 293 00:16:14,200 --> 00:16:15,200 an empty list. 294 00:16:16,600 --> 00:16:19,500 So after reading your extension what I am doing is, I am 295 00:16:19,500 --> 00:16:22,200 defining an empty list. That is, 296 00:16:23,500 --> 00:16:24,500 let me take 297 00:16:25,800 --> 00:16:30,500 'req_files = []'. 298 00:16:30,500 --> 00:16:32,300 [no audio] 299 00:16:32,300 --> 00:16:33,300 Instead of printing 300 00:16:33,300 --> 00:16:37,600 what I am doing is, inside of your for loop I am appending 301 00:16:37,600 --> 00:16:43,000 your each and every, your each and every file into your list. 302 00:16:43,900 --> 00:16:46,800 If you remember there is 'append' operation with your list. 303 00:16:47,200 --> 00:16:51,000 The purpose of 'append' operation is adding your given values 304 00:16:51,000 --> 00:16:53,500 to your list. That's it. 305 00:16:54,700 --> 00:16:59,300 Right. And then after completion of your for loop, I am going 306 00:16:59,300 --> 00:17:03,100 to print finally, "The required", 307 00:17:04,500 --> 00:17:10,200 see the result, "The required files are", as a list I am printing. 308 00:17:10,200 --> 00:17:12,500 [no audio] 309 00:17:12,500 --> 00:17:15,400 You have taken 'req_files'. 310 00:17:15,400 --> 00:17:17,500 [no audio] 311 00:17:17,500 --> 00:17:21,700 'r-e-q', right. And I did a mistake in your loop, 312 00:17:22,000 --> 00:17:25,800 let me go and modify that variable name as 'req'. 313 00:17:25,800 --> 00:17:27,800 [no audio] 314 00:17:27,800 --> 00:17:28,800 Right. Fine. 315 00:17:28,900 --> 00:17:34,000 Let me save it, and run it. I'm running. I'm going to provide 316 00:17:34,000 --> 00:17:35,099 a path called, 317 00:17:36,200 --> 00:17:37,300 Where is my path? 318 00:17:37,400 --> 00:17:40,000 You can take any path, right. As of now, I am giving this path, 319 00:17:41,599 --> 00:17:45,500 and I'm giving '.py' extension. See the result. Now instead 320 00:17:45,500 --> 00:17:47,300 of printing your files separately, 321 00:17:47,300 --> 00:17:50,200 you are getting as a list of your required files. 322 00:17:50,700 --> 00:17:54,000 Not only this, same script will work for any type of files. 323 00:17:55,800 --> 00:17:58,800 Let me enter, and see the result. 324 00:17:59,000 --> 00:18:00,000 I'm entering suppose 325 00:18:00,000 --> 00:18:03,800 '.sh'. Yes. fine. Guys 326 00:18:03,800 --> 00:18:08,600 now final concept is, I am giving a path called this one. 327 00:18:08,600 --> 00:18:10,900 [no audio] 328 00:18:10,900 --> 00:18:17,800 Suppose I want to get a list of some batch files. See the 329 00:18:17,800 --> 00:18:22,800 result. "The required files are: []", you are getting because 330 00:18:22,800 --> 00:18:27,500 there are no '.bat' files in my given this directory. 331 00:18:28,500 --> 00:18:29,600 See the result. Here 332 00:18:29,600 --> 00:18:32,200 we don't have anything which is ending with '.bat'. 333 00:18:32,900 --> 00:18:37,000 If I don't find any files ending with '.bat' in the given 334 00:18:37,000 --> 00:18:39,300 location, I don't want to print in this way. 335 00:18:39,300 --> 00:18:43,000 I want to print like, "There are no .bat files in the given 336 00:18:43,000 --> 00:18:44,900 location", instead of printing an empty. 337 00:18:45,600 --> 00:18:48,700 Nothing is there. It's very simple. See that. What I am doing 338 00:18:48,700 --> 00:18:56,700 is, before printing, before printing your required files from this list 339 00:18:56,700 --> 00:19:03,900 I am checking whether that, whether that is having 0 values 340 00:19:04,000 --> 00:19:08,800 or not, 'len(req_files)' if it is equal to 0, 341 00:19:09,100 --> 00:19:12,100 then I want to print, 'print( 342 00:19:12,100 --> 00:19:14,300 [no audio] 343 00:19:14,300 --> 00:19:16,100 "There are no", 344 00:19:16,100 --> 00:19:19,000 [no audio] 345 00:19:19,000 --> 00:19:20,700 whatever the extension you are providing, 346 00:19:22,000 --> 00:19:23,200 that extension files, 347 00:19:23,200 --> 00:19:26,200 [no audio] 348 00:19:26,200 --> 00:19:30,600 "files in the location" 349 00:19:30,600 --> 00:19:33,000 [no audio] 350 00:19:33,000 --> 00:19:34,000 "in the location 351 00:19:34,000 --> 00:19:37,900 of", you can write your message in any way. 352 00:19:37,900 --> 00:19:40,600 What is your required given path? This one, right? 353 00:19:40,600 --> 00:19:44,200 [no audio] 354 00:19:44,200 --> 00:19:45,200 That's it. 355 00:19:46,400 --> 00:19:51,200 'else', 'else' then only, 'else' means not equal to 0, means 356 00:19:51,200 --> 00:19:53,200 there are some files. Now I am printing that. 357 00:19:53,200 --> 00:19:55,100 [no audio] 358 00:19:55,100 --> 00:19:56,600 See the result. Now 359 00:19:56,600 --> 00:19:58,200 I'm going to run your script. 360 00:19:58,400 --> 00:19:59,800 Let me copy your path. 361 00:20:00,400 --> 00:20:03,000 I'm giving your path. Extension 362 00:20:03,000 --> 00:20:04,500 I'm giving '.bat'. 363 00:20:05,900 --> 00:20:10,100 There are no '.bat' files in the location of your, that one. 364 00:20:10,600 --> 00:20:14,200 Let me rerun and provide '.py' extensions. 365 00:20:14,200 --> 00:20:15,200 Yes, you are getting. 366 00:20:15,200 --> 00:20:17,100 [no audio] 367 00:20:17,100 --> 00:20:20,700 Right. But, one more just for your decoration 368 00:20:20,700 --> 00:20:22,500 purpose, I want to write one more line. 369 00:20:22,500 --> 00:20:25,500 That is, if you have some values, I mean if you have 370 00:20:25,500 --> 00:20:28,300 some '.py' files, how many files you have in that? 371 00:20:29,400 --> 00:20:30,500 Right. See nothing 372 00:20:30,500 --> 00:20:32,000 is there. Again, it's very simple. 373 00:20:32,000 --> 00:20:33,800 [no audio] 374 00:20:33,800 --> 00:20:36,700 I am printing with one more 'print' statement, 'print( 375 00:20:36,700 --> 00:20:39,200 [no audio] 376 00:20:39,200 --> 00:20:40,200 "there are", 377 00:20:41,600 --> 00:20:45,400 "There are ")'. 378 00:20:46,300 --> 00:20:48,400 That means it is going to give a count, how many values 379 00:20:48,400 --> 00:20:49,800 are there in that list. 380 00:20:51,000 --> 00:20:57,200 "There are", this many number of files, "files in the location of", 381 00:20:57,200 --> 00:21:02,200 [no audio] 382 00:21:02,200 --> 00:21:05,500 required path, "with an extension of", 383 00:21:05,500 --> 00:21:09,000 [no audio] 384 00:21:09,000 --> 00:21:10,000 whatever the given 385 00:21:10,000 --> 00:21:12,800 extension, right, that is there in this. 386 00:21:12,800 --> 00:21:14,600 [no audio] 387 00:21:14,600 --> 00:21:16,800 That's it. Fine. 388 00:21:17,900 --> 00:21:20,000 And, "They are", now instead of writing 389 00:21:20,000 --> 00:21:22,700 "The required files are", I can write, 390 00:21:22,700 --> 00:21:24,800 [no audio] 391 00:21:24,800 --> 00:21:29,700 "So, the files are". "So, the files are". That's it. 392 00:21:30,700 --> 00:21:32,100 Let me save it and run it. 393 00:21:32,100 --> 00:21:34,400 [no audio] 394 00:21:34,400 --> 00:21:36,900 I am giving a path of this one, now 395 00:21:36,900 --> 00:21:43,300 see the magic. '.py'. "There are 5 files in the location 396 00:21:43,300 --> 00:21:46,400 of", "with an extension .py". Then, 397 00:21:46,600 --> 00:21:49,700 "So, the files are", you are getting those five files. 398 00:21:50,600 --> 00:21:55,200 Not only this, right, you can provide any path and any file 399 00:21:55,200 --> 00:21:58,900 extension, automatically now our Python script will fetch them. 400 00:22:00,100 --> 00:22:03,100 Only two files and we are getting them. Right. 401 00:22:03,200 --> 00:22:07,700 So guys, this is the simple way to get your required files 402 00:22:07,900 --> 00:22:10,500 with your required extension in the given location. 403 00:22:11,500 --> 00:22:14,400 See, here maybe concepts are simple, 404 00:22:14,400 --> 00:22:18,000 but the thing is, you have to understand that why we are taking 405 00:22:18,000 --> 00:22:21,400 first 'if-else' block, and why we are writing the code only 406 00:22:21,400 --> 00:22:24,200 inside of your 'else' block of your first 'if' condition. 407 00:22:25,600 --> 00:22:27,300 First this is our main code guys. 408 00:22:27,500 --> 00:22:30,800 If you want to understand this code you have to understand in this way. 409 00:22:32,100 --> 00:22:34,800 If you run, your code will execute from top to down, right. 410 00:22:35,600 --> 00:22:37,000 You have three lines, main, 411 00:22:37,000 --> 00:22:43,700 sorry, one, two, three, four, five, main lines. 412 00:22:45,100 --> 00:22:48,100 In the five main lines whenever if you run your script, so 413 00:22:48,100 --> 00:22:51,000 by this line, your operating system will understand that 414 00:22:51,000 --> 00:22:54,300 the remaining code has to run with the help of your Python 3 415 00:22:54,300 --> 00:22:55,900 Then you are importing 'os'. 416 00:22:56,000 --> 00:22:57,400 Then you are reading your path. 417 00:22:57,900 --> 00:23:00,600 Then you commented this. Don't worry about this line. Then 418 00:23:00,600 --> 00:23:04,500 'if' and 'else', so first of all, you have to understand that, 'if' condition 419 00:23:04,500 --> 00:23:07,400 is 'True', this line will execute, 'if' condition is 'False', this 420 00:23:07,400 --> 00:23:10,600 entire block is going to execute. But inside that again we 421 00:23:10,600 --> 00:23:12,400 have one more condition, that's a different story. 422 00:23:13,300 --> 00:23:16,100 If given path is a file, then you are going to execute this 423 00:23:16,100 --> 00:23:17,700 line. Then you don't, then 424 00:23:17,700 --> 00:23:19,300 you don't have any lines to execute. 425 00:23:20,900 --> 00:23:23,500 If this is 'False' means, given path is directory. 426 00:23:23,500 --> 00:23:25,600 [no audio] 427 00:23:25,600 --> 00:23:29,000 Right. Then you are going to execute these lines. 428 00:23:29,000 --> 00:23:31,700 [no audio] 429 00:23:31,700 --> 00:23:32,700 Right. 430 00:23:34,000 --> 00:23:37,100 Then you are finding the number of files and directories 431 00:23:37,100 --> 00:23:39,900 which are there in the given path, right. 432 00:23:40,600 --> 00:23:44,200 Then maybe you have some files and directories in the given 433 00:23:44,200 --> 00:23:46,900 location or you may not have anything in that, that's why 434 00:23:46,900 --> 00:23:51,700 I'm checking, 'if', all files and directories in the given location, 435 00:23:52,300 --> 00:23:56,600 the number of values, if equals to 0, no need to find the extension 436 00:23:57,900 --> 00:23:59,300 files. Right. 437 00:23:59,300 --> 00:24:03,100 That's why 'if' and 'else'. 'if', number of values, I mean number 438 00:24:03,100 --> 00:24:05,900 of files and directories are equal to 0, then you are printing this one, 439 00:24:05,900 --> 00:24:06,900 then your script is over. 440 00:24:07,400 --> 00:24:10,400 If it is not equal to 0 means, there are something, maybe files 441 00:24:10,400 --> 00:24:12,100 or directories or both. No problem. 442 00:24:12,900 --> 00:24:15,800 So if there are something then only I want to read extension. 443 00:24:15,800 --> 00:24:17,000 Yes, I am reading that. 444 00:24:17,000 --> 00:24:19,000 [no audio] 445 00:24:19,400 --> 00:24:20,700 Right. So this is just variable. 446 00:24:20,700 --> 00:24:22,200 You're defining an empty list. 447 00:24:23,400 --> 00:24:26,500 Then, you're going to get some number of files and directories 448 00:24:26,500 --> 00:24:29,700 in this, right. Now my intention is I want to take one by one, 449 00:24:29,700 --> 00:24:32,700 and then I want to check whether this is ending with your 450 00:24:32,700 --> 00:24:33,900 required extension or not. 451 00:24:33,900 --> 00:24:36,700 If it is ending with required extension, then I am storing 452 00:24:36,700 --> 00:24:39,700 that value into this list. You know in list you can store 453 00:24:39,700 --> 00:24:40,900 any number of values. 454 00:24:42,300 --> 00:24:45,500 Right. After completion of your for loop what I am doing? 455 00:24:45,500 --> 00:24:47,500 [no audio] 456 00:24:47,500 --> 00:24:52,200 Right. 'if len(req_files)', whatever the files 457 00:24:52,200 --> 00:24:57,000 you are finding. If it is equal to 0 means, there are no '.py' 458 00:24:57,000 --> 00:25:00,500 or '.sh', whatever may be your extension, with that extension 459 00:25:00,500 --> 00:25:01,700 you don't have any files. 460 00:25:02,500 --> 00:25:05,900 That's why I am printing this information. 'else' means, you 461 00:25:05,900 --> 00:25:08,200 have some files, then I am printing that, how many files are 462 00:25:08,200 --> 00:25:12,800 there, and what are those. That's it. And guys in this entire 463 00:25:12,800 --> 00:25:17,200 program if you provide valid path then only it will work. Suppose 464 00:25:17,300 --> 00:25:20,600 if you enter an invalid path, see that. First of all 465 00:25:20,600 --> 00:25:23,200 [no audio] 466 00:25:23,200 --> 00:25:25,000 I am entering now path as, 467 00:25:25,000 --> 00:25:27,800 [no audio] 468 00:25:27,800 --> 00:25:30,600 something. See that. 469 00:25:31,800 --> 00:25:33,500 "No such file or directory". 470 00:25:33,500 --> 00:25:36,000 [no audio] 471 00:25:36,000 --> 00:25:40,100 Right. That's why you have to provide valid file. In case if you 472 00:25:40,100 --> 00:25:43,700 provide invalid file or invalid path, then how to take 473 00:25:43,700 --> 00:25:46,200 care? We will see that while going forward. 474 00:25:46,200 --> 00:25:48,000 As of now while running this script 475 00:25:48,000 --> 00:25:52,000 you have to provide valid path only. It maybe directory path 476 00:25:52,100 --> 00:25:53,100 or file path. 477 00:25:53,500 --> 00:25:56,800 If you provide file path, then your script will stop and 478 00:25:56,800 --> 00:25:59,700 say that, "This is a file path, please pass only directory 479 00:25:59,700 --> 00:26:03,800 path". If you provide valid directory path, then it will work 480 00:26:03,800 --> 00:26:04,900 on your given path. 481 00:26:05,900 --> 00:26:07,200 Right. So 482 00:26:08,500 --> 00:26:11,100 if you provide any invalid path how to handle, we will 483 00:26:11,100 --> 00:26:12,400 see that while going forward. 484 00:26:12,500 --> 00:26:15,300 I don't want to confuse you in a single video with all those concepts. 485 00:26:16,300 --> 00:26:17,300 Right. Fine. 486 00:26:17,600 --> 00:26:19,400 Okay guys, thank you for watching this video. 487 00:26:19,400 --> 00:26:27,398 [no audio]