1 00:00:00,000 --> 00:00:01,800 [no audio] 2 00:00:01,800 --> 00:00:03,400 Friends, here we are going to write 3 00:00:03,429 --> 00:00:06,200 one more Python script. Requirement 4 00:00:06,200 --> 00:00:11,300 is, read a directory path and then identify all files and 5 00:00:11,300 --> 00:00:13,000 directories in the given path. 6 00:00:14,400 --> 00:00:19,500 See simply our script should read some path, directory 7 00:00:19,500 --> 00:00:24,400 path. So in that location, in that given path, what are all the 8 00:00:24,400 --> 00:00:27,000 directories and files are there, right? 9 00:00:27,100 --> 00:00:30,600 I want to display all those things by mentioning, 10 00:00:30,600 --> 00:00:32,299 this is a file and this is a directory. 11 00:00:32,700 --> 00:00:34,800 Let me take one simple location. 12 00:00:36,300 --> 00:00:37,700 Yeah, see here. 13 00:00:38,600 --> 00:00:41,600 I have to provide this as a path to my Python script. Then 14 00:00:41,600 --> 00:00:43,300 my Python script has to display 15 00:00:44,000 --> 00:00:48,100 'mydir' is a directory, 'New Text Document' is a file, 16 00:00:48,300 --> 00:00:53,400 'xyz' is a file. Your script is able to identify, it has to display, 17 00:00:53,400 --> 00:00:56,900 this is a directory, and this is a file, and this is also a file. 18 00:00:58,000 --> 00:01:01,100 Right. Let me go and write one by one. For time being 19 00:01:01,100 --> 00:01:05,000 what I am doing is, let me remove one file here just for simplicity 20 00:01:05,000 --> 00:01:07,900 purpose. Finally we'll write it. 21 00:01:08,800 --> 00:01:09,900 Let me save it. 22 00:01:09,900 --> 00:01:12,300 [no audio] 23 00:01:12,300 --> 00:01:16,800 'read_a_path_and_identify_files_and_dirs'. 24 00:01:18,300 --> 00:01:19,400 Fine. See here. 25 00:01:19,400 --> 00:01:23,900 [no audio] 26 00:01:23,900 --> 00:01:27,600 Guys first thing, we need to read a path, right? 27 00:01:27,700 --> 00:01:32,600 So to read a path because path is a string, in your Python 28 00:01:32,600 --> 00:01:38,100 you have 'input' to read any string. "Enter your directory path: ". That's it. 29 00:01:38,800 --> 00:01:40,800 [no audio] 30 00:01:40,800 --> 00:01:44,400 After reading that path, first of all I want to display 31 00:01:44,400 --> 00:01:48,300 all the list of files and directories in the given location. 32 00:01:49,500 --> 00:01:53,100 Right. If you remember, there is an operation from your 'os' 33 00:01:53,100 --> 00:01:56,800 module that is 'os.listdir'. 34 00:01:57,300 --> 00:02:00,500 So now here I have a requirement with 'os' module, 35 00:02:00,700 --> 00:02:02,900 that's why importing 'os'. 36 00:02:04,400 --> 00:02:06,100 Now simply I am printing 37 00:02:06,100 --> 00:02:10,100 'os.listdir', and whatever the path you are providing 38 00:02:10,199 --> 00:02:11,500 that path I am taking. 39 00:02:12,900 --> 00:02:14,700 Right. Let me go to 40 00:02:16,200 --> 00:02:18,300 my command line. From there 41 00:02:18,300 --> 00:02:21,100 I want to run, because I need to provide this path, right. 42 00:02:21,600 --> 00:02:25,900 So our script location is in this somewhere. First 43 00:02:25,900 --> 00:02:27,600 let me open your script. 44 00:02:28,800 --> 00:02:30,900 This is your script, only we have three lines. 45 00:02:31,000 --> 00:02:33,900 First line is, you are importing 'os' module. Through second 46 00:02:33,900 --> 00:02:38,100 line you are going to read from your command line, your path. 47 00:02:38,400 --> 00:02:39,800 Third line, in the given path 48 00:02:39,800 --> 00:02:42,400 you're going to list out all the directories and files. 49 00:02:43,100 --> 00:02:44,100 Let me do it. 50 00:02:44,100 --> 00:02:46,800 [no audio] 51 00:02:46,800 --> 00:02:48,800 Right, and I want to provide a path. 52 00:02:48,800 --> 00:02:50,600 Let me take this as my path. 53 00:02:51,700 --> 00:02:55,800 And I am providing, see guys this script will work both on your 54 00:02:55,900 --> 00:03:00,700 Windows as well as Unix-like systems, but while providing path 55 00:03:00,900 --> 00:03:04,700 on Windows operating system you have to provide '\\' 56 00:03:04,700 --> 00:03:08,100 in the path. See the '\', we have single 57 00:03:08,100 --> 00:03:11,600 '\', no, but you have to provide '\\' just to avoid whenever 58 00:03:11,600 --> 00:03:15,500 if you have some special characters in a path. Right. Only 59 00:03:15,500 --> 00:03:17,700 whenever if you are providing manually at that time only you have 60 00:03:17,700 --> 00:03:22,500 to provide. Fine. Now see the output. This path, whatever you're 61 00:03:22,500 --> 00:03:25,700 providing, that is going to store in this variable, and I am 62 00:03:25,700 --> 00:03:30,400 listing 'os.path.', 'os.listdir' in your 'path'. 63 00:03:30,900 --> 00:03:33,400 So in this location, I want to list all the directories and 64 00:03:33,400 --> 00:03:37,000 files. And this output is a list. See that. Already we have 65 00:03:37,000 --> 00:03:38,900 seen in our 'os' module explanation 66 00:03:39,200 --> 00:03:42,500 this is always a list. Now you are getting two files, 67 00:03:43,700 --> 00:03:49,000 one is directory and one more is file. But my requirement 68 00:03:49,000 --> 00:03:52,700 is, if I get some values in the given path, I mean some files 69 00:03:52,700 --> 00:03:54,900 and directories, our Python script has to display, 70 00:03:55,300 --> 00:03:57,700 this is a directory and this is a file. 71 00:03:57,700 --> 00:03:59,600 [no audio] 72 00:03:59,600 --> 00:04:03,700 Right. So, you know, if you know the path, if you know the path of 73 00:04:03,700 --> 00:04:07,400 your file, then you can find out whether this is a file or 74 00:04:07,400 --> 00:04:13,600 not using your 'os.path.isfile', 'isdir'. 75 00:04:13,600 --> 00:04:15,900 You can take any one of that operation if you remember. 76 00:04:17,300 --> 00:04:23,000 Anyway before going to check whether it is a file or not, 77 00:04:23,000 --> 00:04:26,899 first of all you should have in your hand your complete 78 00:04:26,899 --> 00:04:28,800 path of your file or directory. 79 00:04:29,400 --> 00:04:31,800 Otherwise, you will get an error. Because your script you 80 00:04:31,800 --> 00:04:34,000 are running from this location, but these files are there 81 00:04:34,000 --> 00:04:35,100 in a different location. 82 00:04:36,100 --> 00:04:38,500 Right. See that. Suppose if I do simply. 83 00:04:38,500 --> 00:04:41,000 [no audio] 84 00:04:41,000 --> 00:04:42,500 Yeah, one more thing. Before that 85 00:04:42,600 --> 00:04:48,100 let me store this output into one variable, files and directories, 86 00:04:49,300 --> 00:04:53,000 files and directories list, 'df_l'. That's it. 87 00:04:53,700 --> 00:04:55,300 Now, I can also print in this way, right? 88 00:04:55,300 --> 00:04:58,600 [no audio] 89 00:04:58,600 --> 00:05:00,100 Right. Let me rerun your code. 90 00:05:01,300 --> 00:05:02,300 So this is your code. 91 00:05:02,300 --> 00:05:03,600 Now, I am going to rerun that. 92 00:05:04,200 --> 00:05:05,600 I am going to provide this path. 93 00:05:05,800 --> 00:05:08,800 Yes, same output you are getting because of this 'print' line. 94 00:05:09,400 --> 00:05:13,100 So in this variable, now you have these two things. As of 95 00:05:13,100 --> 00:05:15,700 now assume that whatever the path you are going to provide, 96 00:05:15,700 --> 00:05:20,700 in that path you have any two files, or any two directories, or any one 97 00:05:20,700 --> 00:05:25,500 directory, and one file. Now to identify if this is a file or not, 98 00:05:25,700 --> 00:05:26,900 and this is a file or not, 99 00:05:26,900 --> 00:05:30,600 first of all I have to separate this value from this list, right? 100 00:05:31,300 --> 00:05:35,300 So that's why what I am taking is, I am creating two paths. 101 00:05:35,400 --> 00:05:36,700 Now, let me comment this line. 102 00:05:38,300 --> 00:05:44,600 Path 1, let me take 'p1', path 1 '=', from your this list 103 00:05:44,600 --> 00:05:49,900 index 0 I am storing into 'p1'. 'p2=', from 104 00:05:49,900 --> 00:05:58,000 your this one, index 1. Right. Now I want to identify whether 105 00:05:58,000 --> 00:06:06,000 'p1' is a, whether 'p1' is a file or not, file or directory. Guys 106 00:06:07,000 --> 00:06:11,300 let me do it. As per your output, 'p2' is a file, right? 107 00:06:11,400 --> 00:06:13,700 See because second value is a file, index 1 value. 108 00:06:13,900 --> 00:06:19,100 Let me check it first on your 'p2'. 'If', you know in our previous 109 00:06:19,100 --> 00:06:23,400 example also we have seen 'os.path.isfile(p2)'. 110 00:06:25,000 --> 00:06:28,500 Then I can say your 'p2', 111 00:06:29,600 --> 00:06:33,500 sorry, I need to provide here, 'p2', right. We're working on 'p2'. 'p2' is 112 00:06:33,500 --> 00:06:37,900 a file. 'else', if that is 'False', 113 00:06:38,100 --> 00:06:39,900 'os.path.isfile', 114 00:06:39,900 --> 00:06:43,000 maybe 'p2' is a directory or file, you don't know. If you 115 00:06:43,000 --> 00:06:44,500 know exactly you can say that that is a file, 116 00:06:44,500 --> 00:06:47,300 otherwise that is a directory, right. Then I can say 117 00:06:48,800 --> 00:06:53,300 your 'p2' is a directory. 118 00:06:54,800 --> 00:06:58,500 Let me save it, and go to command line, and then read your 119 00:06:58,500 --> 00:07:01,200 script once. Let me run your script. Now, 120 00:07:01,200 --> 00:07:06,500 I am providing some path. See 'xyz.sh is a directory', you're 121 00:07:06,500 --> 00:07:07,900 getting, but that is not correct. 122 00:07:09,100 --> 00:07:11,100 The reason why you are getting is, 123 00:07:11,100 --> 00:07:13,400 [no audio] 124 00:07:13,400 --> 00:07:16,000 actually 'p2' means 'xyz.sh'. 125 00:07:17,200 --> 00:07:21,000 Your Python script you're running from different location, 126 00:07:21,300 --> 00:07:23,200 and your files are there in a different location. 127 00:07:23,200 --> 00:07:27,800 That's why whenever if you are running any path related operation 128 00:07:28,000 --> 00:07:31,800 you should get in your hand complete path. As of now 129 00:07:31,800 --> 00:07:33,300 you don't have a complete path. 'xyz' 130 00:07:33,300 --> 00:07:36,200 is there somewhere in different location, but I don't have 131 00:07:36,200 --> 00:07:37,300 complete path for that. 132 00:07:38,000 --> 00:07:42,100 Right. What is the complete path? Complete path is nothing but, in 133 00:07:42,100 --> 00:07:45,600 the given location, in this given location your file is there, 134 00:07:46,000 --> 00:07:51,000 then for this location at the end, at the end if you add 135 00:07:51,000 --> 00:07:54,600 'xyz.sh', then that is the complete path for this file. 136 00:07:55,900 --> 00:07:59,900 Right. So that you have to create first. How you can create it? 137 00:08:01,100 --> 00:08:04,100 See if you remember, 138 00:08:05,500 --> 00:08:10,600 'os.path.join()', your given path, your file, 139 00:08:10,600 --> 00:08:12,900 so that you are going to create a complete path for your file. 140 00:08:14,300 --> 00:08:16,600 See, let me print your path, only path. 141 00:08:17,500 --> 00:08:18,700 I am printing only 'p2' file. 142 00:08:18,700 --> 00:08:20,300 Let me comment your remaining code. 143 00:08:20,300 --> 00:08:22,900 [no audio] 144 00:08:22,900 --> 00:08:26,200 Now see the output what you're going to get. I am running. 145 00:08:27,300 --> 00:08:29,000 You're getting complete path for your file, 146 00:08:29,000 --> 00:08:33,299 no. Once if we get complete path, guys complete path is nothing 147 00:08:33,299 --> 00:08:35,400 but your given path, in that path 148 00:08:35,400 --> 00:08:38,000 you are having 'xyz.sh'. That's why if I add these two 149 00:08:38,000 --> 00:08:42,900 things, you can get complete path. Your 'xyz.sh', and given path, 150 00:08:42,900 --> 00:08:45,799 if you add those two things, you will get complete path for 151 00:08:45,799 --> 00:08:49,700 your file. So that you can directly, irrespective of your operating 152 00:08:49,700 --> 00:08:51,900 system to add two paths you 153 00:08:51,900 --> 00:08:55,000 can use directly 'os.path.join'. That's what I did. 154 00:08:55,799 --> 00:08:57,500 Not only for 'p2', 'p1' 155 00:08:57,500 --> 00:08:59,600 also you have to do. Not only for that file, 156 00:09:00,200 --> 00:09:02,400 if you have multiple things, for all things you have to do 157 00:09:02,400 --> 00:09:05,400 in this way only. First of all, you have to create complete 158 00:09:05,400 --> 00:09:06,900 path for your file. 159 00:09:06,900 --> 00:09:09,000 [no audio] 160 00:09:09,000 --> 00:09:11,600 Guys, why I am taking your path only, because that is the 161 00:09:11,600 --> 00:09:13,800 path you are providing as the input. In that path 162 00:09:13,800 --> 00:09:16,100 only your file is there. That's why if you add those 163 00:09:16,100 --> 00:09:18,000 two things, you can get complete path. 164 00:09:18,000 --> 00:09:20,200 [no audio] 165 00:09:20,200 --> 00:09:23,500 Fine. Now, run your script. Now, 166 00:09:23,500 --> 00:09:26,000 I am going to comment your two files. 167 00:09:26,000 --> 00:09:27,900 [no audio] 168 00:09:27,900 --> 00:09:31,100 Right. Now see that. As of now 169 00:09:31,100 --> 00:09:33,600 I have a if-else condition only for 'p2' path. 170 00:09:33,900 --> 00:09:37,300 Let me verify that whether 'p2' is working perfectly or not. 171 00:09:38,100 --> 00:09:39,500 This is the path. Yes. 172 00:09:40,600 --> 00:09:43,500 "is a file", you are getting. Same logic 173 00:09:43,500 --> 00:09:45,900 I need to apply for 'p1' also, right? Then what I have to do? 174 00:09:46,300 --> 00:09:49,800 Let me copy this logic once again and try to apply for 'p1'. 175 00:09:49,800 --> 00:09:54,100 [no audio] 176 00:09:54,100 --> 00:09:56,100 See that. I am trying to apply for 'p1'. 177 00:09:56,100 --> 00:09:58,500 [no audio] 178 00:09:58,500 --> 00:10:01,900 So what will happen? If this is a file, this will become 'True', 179 00:10:02,000 --> 00:10:04,400 else 'False'. 'False' means, you're going to display this one. 180 00:10:05,500 --> 00:10:08,000 Guys in your previous script 181 00:10:08,000 --> 00:10:13,600 we also used 'os.path.exist', but here no need of that 182 00:10:13,600 --> 00:10:17,200 because if a given path is valid path, then only you are going 183 00:10:17,200 --> 00:10:21,800 to get list of files, 'listdir' into this 'df_l' variable. 184 00:10:23,000 --> 00:10:26,400 If you provide valid path then only, right, then only you are going to get it. 185 00:10:26,900 --> 00:10:29,100 If you want to write effective script here you can apply 186 00:10:30,100 --> 00:10:34,000 'if os.path.exist'. 187 00:10:34,100 --> 00:10:38,000 If given path is a valid path, then only I can find the list 188 00:10:38,000 --> 00:10:39,700 of directories and files in the given thing. 189 00:10:39,800 --> 00:10:45,400 Otherwise, I can write 'else'. 'else', 'print("please provide valid path")' 190 00:10:45,400 --> 00:10:51,400 [no audio] 191 00:10:51,400 --> 00:10:55,900 Right. And one more thing, anyway it's time to give that as 192 00:10:55,900 --> 00:11:00,100 well. If path is invalid you are not going to get this list. 193 00:11:00,800 --> 00:11:04,100 If you're not going to get this list, how can you, how can 194 00:11:04,100 --> 00:11:05,300 you create 'p1', 'p2'? 195 00:11:05,400 --> 00:11:09,400 You can't create. That's why if this path is not existing, 196 00:11:09,400 --> 00:11:12,400 you have to print this information and you should not execute 197 00:11:12,400 --> 00:11:14,200 this code. If you're executing 198 00:11:14,200 --> 00:11:17,300 this code means, you got your 'df_l' variable. 199 00:11:17,700 --> 00:11:21,200 Now, you are not getting. If path is invalid, you can't get this, 200 00:11:21,400 --> 00:11:22,900 because this is false, right? 201 00:11:23,900 --> 00:11:26,600 Without having your list, how can you use in your code? 202 00:11:26,600 --> 00:11:30,200 That's why you have to stop your script. To stop your script 203 00:11:30,200 --> 00:11:32,300 you have a syntax called 'sys.exit'. 204 00:11:33,300 --> 00:11:39,000 Now we are using 'sys' module, so you have to import that. So 205 00:11:39,000 --> 00:11:42,400 without 'sys.exit' if I run my code, see the output, how 206 00:11:42,400 --> 00:11:43,300 you're going to get. 207 00:11:43,300 --> 00:11:45,800 [no audio] 208 00:11:45,800 --> 00:11:49,000 You will get an error. See that. Instead of this, 209 00:11:49,100 --> 00:11:53,700 I am giving, "invalid path". See, if it is invalid path, you are 210 00:11:53,700 --> 00:11:54,900 not getting 'df_l'. 211 00:11:54,900 --> 00:11:56,300 If you are not getting 'df_l', 212 00:11:56,300 --> 00:11:58,300 how can you get from that index zero? 213 00:11:59,300 --> 00:12:01,100 You can't create 'p1', right. Path one 214 00:12:01,100 --> 00:12:04,200 you can't create by taking index 0 from your variable. 215 00:12:05,400 --> 00:12:08,600 If given path is, "please provide valid path", you are getting, 216 00:12:08,600 --> 00:12:10,300 that means given path is invalid. 217 00:12:10,400 --> 00:12:14,200 If it is invalid, you can't get this one. 218 00:12:14,900 --> 00:12:18,500 The reason is, if it is invalid path this will become 'False'. 219 00:12:18,500 --> 00:12:22,000 If it is 'False', there is no way to create your 'df_l'. 220 00:12:22,000 --> 00:12:23,900 If there is no 'df_l', 221 00:12:24,100 --> 00:12:27,400 how can you take 'p1', 'p2'? How can you create 'p1' and 'p2' from 222 00:12:27,400 --> 00:12:28,600 this 'df_l'? 223 00:12:29,000 --> 00:12:30,000 You don't have that. 224 00:12:30,000 --> 00:12:32,600 That's why I need to stop my script. 225 00:12:32,600 --> 00:12:34,900 [no audio] 226 00:12:34,900 --> 00:12:40,100 If it is valid path, then only I need to run my script. So 227 00:12:40,100 --> 00:12:43,400 if it is valid, your 'if' condition will become 'True', this value will execute, 228 00:12:43,400 --> 00:12:46,800 and anyway if this is 'True', you know that 'else' is going 229 00:12:46,800 --> 00:12:52,100 to skip by your Python. Now see same code 230 00:12:52,100 --> 00:12:54,300 I'm running and I am providing invalid path. 231 00:12:54,500 --> 00:12:56,900 Now, you are not getting any error, instead of that you are getting 232 00:12:56,900 --> 00:12:59,300 simply, "please provide valid path". 233 00:13:01,000 --> 00:13:04,800 Okay. It's good because here we got one more concept how 234 00:13:04,800 --> 00:13:07,300 to use 'sys.exit'. That's fine. 235 00:13:09,100 --> 00:13:11,700 Now, let me run this code and I am going to provide valid 236 00:13:11,700 --> 00:13:14,800 path. Now see that, in that path, this is a directory. 237 00:13:14,800 --> 00:13:17,700 Yes, you are getting through Python, "is a directory", and this 238 00:13:17,700 --> 00:13:18,700 you're getting, "is a file". 239 00:13:18,700 --> 00:13:20,500 [no audio] 240 00:13:20,500 --> 00:13:22,900 Right. Fine. 241 00:13:22,900 --> 00:13:25,200 [no audio] 242 00:13:25,200 --> 00:13:27,000 So we are creating 'p1' and 'p2', 243 00:13:28,100 --> 00:13:32,600 and we are checking one 'if-else' condition for 'p1', path 1, and 244 00:13:32,600 --> 00:13:36,600 second 'if-else' condition for path 2, and here there is a 245 00:13:36,900 --> 00:13:38,400 very big disadvantage. 246 00:13:39,200 --> 00:13:40,200 What is that means, 247 00:13:41,400 --> 00:13:46,000 as of now in the given location you have only two files. 248 00:13:46,000 --> 00:13:49,000 That's why I'm taking path 1 and path 2. Assume that you 249 00:13:49,000 --> 00:13:51,100 don't know how many files and directories are there in the 250 00:13:51,100 --> 00:13:52,500 given directory path. 251 00:13:54,200 --> 00:13:55,500 Let me do one thing. 252 00:13:55,500 --> 00:13:57,500 [no audio] 253 00:13:57,500 --> 00:14:00,100 I am deleting this file. Now 254 00:14:00,100 --> 00:14:01,500 I am going to run your script. 255 00:14:02,700 --> 00:14:04,600 Right. See the result. 256 00:14:04,600 --> 00:14:07,400 [no audio] 257 00:14:07,400 --> 00:14:09,800 You're getting an error, "list index out of range". 258 00:14:10,000 --> 00:14:13,100 The reason is, let me print first your, 259 00:14:13,100 --> 00:14:15,300 [no audio] 260 00:14:15,300 --> 00:14:18,700 Because as of know, you are having only one item in the given 261 00:14:18,700 --> 00:14:20,600 location. See that output. 262 00:14:20,600 --> 00:14:24,300 [no audio] 263 00:14:24,300 --> 00:14:26,900 Right. Only directory is there, that's why index zero. Index 264 00:14:26,900 --> 00:14:29,800 one is not there. But for 'p2' you're using index 1. 265 00:14:31,100 --> 00:14:32,600 Then your script is wrong, right? 266 00:14:33,700 --> 00:14:36,100 Or else I will do one more thing. 267 00:14:36,100 --> 00:14:38,100 [no audio] 268 00:14:38,100 --> 00:14:40,000 I will create some random files here. 269 00:14:40,400 --> 00:14:42,200 Let me create some files here. 270 00:14:42,200 --> 00:14:45,100 [no audio] 271 00:14:45,100 --> 00:14:51,100 'touch x.txt y.py z.sh', right. 272 00:14:52,600 --> 00:14:54,200 Now I created three files here. 273 00:14:55,500 --> 00:14:58,900 I created three files. Now for the same path once again 274 00:14:58,900 --> 00:15:01,400 I'm running your Python script. Try to understand guys. 275 00:15:02,300 --> 00:15:04,100 Now you have actually four 276 00:15:05,300 --> 00:15:08,800 items in the path, but your script is giving about only two paths. 277 00:15:08,800 --> 00:15:11,100 [no audio] 278 00:15:11,100 --> 00:15:13,400 Actually you are having four values, right but 279 00:15:13,400 --> 00:15:16,500 your script is saying only about two information, two items, 280 00:15:16,500 --> 00:15:19,100 two paths from your given location. 281 00:15:19,600 --> 00:15:20,900 Then what about remaining? 282 00:15:20,900 --> 00:15:22,800 [no audio] 283 00:15:22,800 --> 00:15:25,600 Why it is giving only two means, as of now in your Python 284 00:15:25,600 --> 00:15:27,900 script you have taken care only for 'p1' and 'p2'. 285 00:15:28,800 --> 00:15:33,300 So now you have a problem with this script. If your given 286 00:15:33,300 --> 00:15:37,400 location is not having two paths, then your script is going 287 00:15:37,400 --> 00:15:41,500 to give an error, or if it is having more number of files 288 00:15:41,500 --> 00:15:43,700 and directories in the given location then your script is not 289 00:15:43,700 --> 00:15:47,200 able to identify all. It is going to identify only two. 290 00:15:47,200 --> 00:15:49,400 [no audio] 291 00:15:49,400 --> 00:15:53,400 That means based on how many files and directories are 292 00:15:53,400 --> 00:15:56,300 there in the given location, based on that dynamically 293 00:15:56,300 --> 00:15:59,500 your script has to work. That is not happening with the script. 294 00:16:00,200 --> 00:16:04,800 Then how you can do it? Then how you can do it? That's important thing. 295 00:16:06,300 --> 00:16:10,400 Right. Now let me comment this code as of now whatever you have. 296 00:16:10,600 --> 00:16:12,400 I will comment entire code first. 297 00:16:12,400 --> 00:16:15,500 [no audio] 298 00:16:15,500 --> 00:16:16,300 Be clear guys. 299 00:16:16,300 --> 00:16:19,400 It's very, very useful in your real-time concept. 300 00:16:19,400 --> 00:16:22,200 [no audio] 301 00:16:22,200 --> 00:16:23,800 I am writing some syntax guys. 302 00:16:24,000 --> 00:16:24,800 Don't worry about that. 303 00:16:24,800 --> 00:16:26,400 Just try to understand the output. 304 00:16:27,000 --> 00:16:31,200 I am writing 'for each in [2, 3, 4] 305 00:16:31,200 --> 00:16:33,100 [no audio] 306 00:16:33,100 --> 00:16:35,000 Then I'm writing 'print'. Okay. 307 00:16:36,300 --> 00:16:39,200 Then 'print("hi")'. 308 00:16:40,000 --> 00:16:42,700 First of all, assume that this is like 'if' condition. 309 00:16:42,700 --> 00:16:46,200 You know, in case if it is 'if' condition, if your condition is 310 00:16:46,200 --> 00:16:48,600 'True', then you are going to execute these two lines. 311 00:16:48,600 --> 00:16:50,900 [no audio] 312 00:16:50,900 --> 00:16:54,200 If it is 'if' condition, and whatever the condition you have 313 00:16:54,200 --> 00:16:57,700 here, if that is 'True', then you are going to execute these two 314 00:16:57,700 --> 00:17:01,900 lines once. But this is not condition, 315 00:17:01,900 --> 00:17:03,200 this is called a loop. 316 00:17:03,700 --> 00:17:06,000 This here, 'for', 'for' is called a loop. 317 00:17:06,700 --> 00:17:08,300 What is the use of loop means, 318 00:17:09,700 --> 00:17:12,400 be clear, because of this loop 319 00:17:13,500 --> 00:17:15,000 whatever the values, sorry, 320 00:17:15,098 --> 00:17:18,800 whatever the lines you have under your loop, those lines 321 00:17:18,800 --> 00:17:21,700 will repeat some certain number of times again and again 322 00:17:21,700 --> 00:17:25,598 immediately. But how many number of times you are going to, 323 00:17:25,598 --> 00:17:29,000 your loop is going to repeat these two lines means, how many 324 00:17:29,000 --> 00:17:32,300 values you have here that many number of times these two 325 00:17:32,300 --> 00:17:33,300 lines will execute. 326 00:17:34,900 --> 00:17:36,000 Right. See that. First 327 00:17:36,000 --> 00:17:39,500 let me run this code and see the output. Here itself 328 00:17:39,500 --> 00:17:41,500 I am running. Because I'm not providing any input 329 00:17:41,500 --> 00:17:43,300 I can run it from Sublime Text itself. 330 00:17:44,500 --> 00:17:47,200 You have 2, 3, 4, three values in your list, right? 331 00:17:47,300 --> 00:17:48,599 That's why "ok", 332 00:17:48,599 --> 00:17:52,200 "hi", that is one output, second time output, third time output. 333 00:17:53,800 --> 00:17:58,600 So you have to understand first thing 'for' is some loop. Loop 334 00:17:58,600 --> 00:18:00,700 means, you know, starting point and ending point same, right? 335 00:18:00,700 --> 00:18:03,700 So you are repeating here only, that's 336 00:18:03,700 --> 00:18:05,000 why that is called simply loop. 337 00:18:05,900 --> 00:18:10,700 Okay. Then whatever the lines are there under your loop, those 338 00:18:10,700 --> 00:18:14,400 lines are repeating certain number of times immediately, first 339 00:18:14,400 --> 00:18:16,700 time, second time, third time. 340 00:18:17,800 --> 00:18:19,900 Immediately it is repeating these two lines. 341 00:18:20,400 --> 00:18:23,000 But how many number of times? Based on here 342 00:18:23,000 --> 00:18:26,900 whatever you're providing in that how many number of values are there. 343 00:18:27,500 --> 00:18:30,200 If it is a list and tuple how many values are there? 344 00:18:30,300 --> 00:18:31,600 You can also write some string. 345 00:18:32,400 --> 00:18:33,700 Let me do it string here. 346 00:18:33,700 --> 00:18:35,900 [no audio] 347 00:18:35,900 --> 00:18:38,600 I am writing simply "Narendra". 348 00:18:38,600 --> 00:18:42,500 [no audio] 349 00:18:42,500 --> 00:18:45,900 See the output. How many characters are there in a given string, 350 00:18:46,100 --> 00:18:49,500 that many number of times these two lines will repeat again 351 00:18:49,500 --> 00:18:50,800 and again immediately. 352 00:18:50,800 --> 00:18:52,800 [no audio] 353 00:18:52,800 --> 00:18:57,600 Right. Then, actually your syntax is 'for', this is any variable, 354 00:18:57,600 --> 00:18:59,900 'each'. 'each' is there no, that is any variable. 355 00:18:59,900 --> 00:19:03,300 You can take like x, y, z, ,p q, r, or your required variable, or 356 00:19:03,300 --> 00:19:05,800 your name also you can take it. That is simply a variable. 357 00:19:06,600 --> 00:19:08,300 Then 'in' is standard keyword. 358 00:19:08,300 --> 00:19:09,500 You have to write 'in' as it is. 359 00:19:09,800 --> 00:19:14,900 So here you can write a list or tuple or a string also. 360 00:19:16,200 --> 00:19:19,200 Right. For time being what I am writing is, I am going to write 361 00:19:19,900 --> 00:19:22,000 a list of values, four values 362 00:19:22,000 --> 00:19:25,200 I am writing. Then these two lines will repeat four times. 363 00:19:26,100 --> 00:19:27,200 No need to write two lines. 364 00:19:27,200 --> 00:19:30,300 You can write 100 lines, or I can write simply one line 365 00:19:30,300 --> 00:19:32,600 also, no problem. Just for our understanding purpose now 366 00:19:32,600 --> 00:19:33,600 I am writing. 367 00:19:34,200 --> 00:19:35,300 "Hello". Only one line 368 00:19:35,300 --> 00:19:39,500 I am writing. Now this line will repeat again and 369 00:19:39,500 --> 00:19:42,300 again immediately four times because you have four values 370 00:19:42,300 --> 00:19:49,200 here. Observe the output first. Yes, you are getting. Now you 371 00:19:49,200 --> 00:19:51,700 defined somewhere variable called 'each', right. Then 372 00:19:51,700 --> 00:19:52,900 what is the use of this? 373 00:19:53,100 --> 00:19:56,300 Actually, that is very, very important. Whether you 374 00:19:56,300 --> 00:19:59,100 are going to use that inside of your for loop or not, 375 00:19:59,100 --> 00:20:02,800 that is your wish. You can also use that in case if you have 376 00:20:02,800 --> 00:20:07,300 a requirement. And one more thing guys, you are repeating this 377 00:20:07,300 --> 00:20:10,200 lines, right, the lines which are there in your loop, if you 378 00:20:10,200 --> 00:20:13,300 repeat once then that is called one iteration. 379 00:20:14,900 --> 00:20:16,200 One iteration. 380 00:20:16,500 --> 00:20:20,100 So now you can say here we have four values, 381 00:20:20,100 --> 00:20:24,500 that's why your loop is iterating four times or your loop 382 00:20:24,500 --> 00:20:27,500 is repeating four times the lines which are there under your 383 00:20:27,500 --> 00:20:32,600 for loop. As of now, you have only one 'print' statement, 384 00:20:33,500 --> 00:20:37,100 and in case if you want to use this variable inside of your 385 00:20:37,100 --> 00:20:39,200 loop, you can use it. See that. What I am doing is, 386 00:20:39,200 --> 00:20:41,500 ("hello", variable), right? 387 00:20:41,500 --> 00:20:45,500 You can print the variable value by using in this way. Now if 388 00:20:45,500 --> 00:20:50,100 I run this, how it is going to execute? We know four times it 389 00:20:50,100 --> 00:20:52,100 is going to execute because four values are there. 390 00:20:53,300 --> 00:20:56,000 Immediately it will execute four times. Do that. Suppose. 391 00:20:56,000 --> 00:21:01,700 I am writing here "before loop", and I am writing here 392 00:21:01,700 --> 00:21:03,600 'print("after loop")'. 393 00:21:03,600 --> 00:21:06,200 [no audio] 394 00:21:06,200 --> 00:21:09,800 So between these "before loop", "after loop", 'print' statements 395 00:21:10,100 --> 00:21:12,500 this line will repeat four times. 396 00:21:13,900 --> 00:21:17,000 Right. First observe the output, how many times it is going to repeat? 397 00:21:18,100 --> 00:21:22,400 "before loop", "after loop", between that you are getting four 398 00:21:22,400 --> 00:21:26,000 times your 'print("hello")', but you are getting 'each' value as 399 00:21:26,000 --> 00:21:27,700 different. Whenever it is repeating 400 00:21:27,700 --> 00:21:32,400 first time, first iteration 'each' is value 2, second iteration 401 00:21:32,400 --> 00:21:37,400 3, third iteration 4, fourth iteration 5. 402 00:21:37,400 --> 00:21:40,000 [no audio] 403 00:21:40,000 --> 00:21:43,400 Right. Then how your Loop is working with your variable 404 00:21:43,400 --> 00:21:44,400 called 'each'. 405 00:21:45,600 --> 00:21:50,500 Nothing is there guys. How your Python is going to execute? As of now 406 00:21:50,500 --> 00:21:54,200 in your script you have only four lines, sorry three lines. Main 407 00:21:54,200 --> 00:21:59,600 lines are three. Main lines are three. They are, let me take it one by one. 408 00:21:59,600 --> 00:22:02,800 [no audio] 409 00:22:02,800 --> 00:22:05,800 This is first line, second line, third line. 410 00:22:05,800 --> 00:22:09,600 But this line is under your loop, under your loop, this 411 00:22:09,600 --> 00:22:12,100 'print' line. Right. Fine. 412 00:22:12,300 --> 00:22:15,700 Then how it is going to execute means, anyway whenever if 413 00:22:15,700 --> 00:22:19,300 you run your script it will execute from top to down. So this line 414 00:22:19,300 --> 00:22:22,000 will execute first, because of that you are getting this output. 415 00:22:22,000 --> 00:22:26,200 Then whenever there is a keyword called 'for' then your Python 416 00:22:26,200 --> 00:22:29,200 will assume that this is a loop, and your Python will decide 417 00:22:29,200 --> 00:22:32,300 that I need to repeat some lines some number of times. 418 00:22:32,800 --> 00:22:36,100 So as of now under your loop you have only one line. Now your 419 00:22:36,100 --> 00:22:38,100 Python will decide that this line 420 00:22:38,100 --> 00:22:41,200 I have to repeat some number of times, that number of times 421 00:22:41,200 --> 00:22:44,000 are how many values you are having here. 422 00:22:44,900 --> 00:22:47,000 Right. But how it is going to execute? 423 00:22:48,000 --> 00:22:49,800 So whenever there is a for loop, 424 00:22:50,800 --> 00:22:53,600 'for' keyword, immediately your Python will look here, 425 00:22:54,900 --> 00:22:58,000 how many values or how many characters if it is a string. 426 00:22:58,200 --> 00:23:00,300 As of now we have a list, right? 427 00:23:00,300 --> 00:23:03,300 Okay, four values are there. Then your Python will take first 428 00:23:03,300 --> 00:23:07,900 value first, and this value it will assign to this variable, then 429 00:23:07,900 --> 00:23:11,700 after assigning whatever the lines you have under your loop 430 00:23:11,900 --> 00:23:17,200 those lines will execute once. No while executing what I am doing? 431 00:23:17,200 --> 00:23:19,500 I am printing 'each' variable also. Then currently what is the 432 00:23:19,500 --> 00:23:21,100 'each' variable? 2 value. That's it. 433 00:23:21,100 --> 00:23:22,300 That's why you're getting this one. 434 00:23:23,100 --> 00:23:28,100 So first iteration is over. Then your Python will immediately 435 00:23:28,300 --> 00:23:32,500 after completion of first iteration, 2 is going to store 'each', 436 00:23:32,500 --> 00:23:33,800 then you are repeating this. 437 00:23:33,800 --> 00:23:37,400 I mean your Python is executing this. Then after completion 438 00:23:37,400 --> 00:23:40,800 of this line your Python will go and check immediately the 439 00:23:40,800 --> 00:23:43,100 next value. Now, whatever the next value 440 00:23:43,100 --> 00:23:46,200 is there again your Python will assign to your 'each' only. 441 00:23:46,200 --> 00:23:50,000 Now your 'each' variable is 3. After assigning next value 442 00:23:50,000 --> 00:23:51,000 to your 'each' variable, 443 00:23:51,300 --> 00:23:53,900 again your Python will execute the lines which are there 444 00:23:53,900 --> 00:23:54,900 under your loop. 445 00:23:55,800 --> 00:23:58,400 But while executing you are printing 'each'. Now currently what 446 00:23:58,400 --> 00:23:59,700 is the 'each' value? Second value. 447 00:23:59,700 --> 00:24:01,000 That's why you're getting this one. 448 00:24:03,400 --> 00:24:05,300 Okay, second iteration is also over. 449 00:24:06,400 --> 00:24:10,500 Then your Python immediately will go for third value. Third 450 00:24:10,500 --> 00:24:11,500 value is 4 no. 451 00:24:11,700 --> 00:24:15,600 Now this 4 we'll assign to 'each' variable. After assigning 452 00:24:15,600 --> 00:24:20,000 third value to 'each', then this logic will repeat third time, 453 00:24:20,300 --> 00:24:21,400 and while repeating third time 454 00:24:21,400 --> 00:24:22,600 what is your value? 4 455 00:24:22,600 --> 00:24:23,800 That's why you're getting this one. 456 00:24:25,100 --> 00:24:28,400 Then it will, Python will look for fourth value. 457 00:24:28,400 --> 00:24:32,300 Yes, 4. Then 4 we'll assign to 'each'. Then we'll execute once. 458 00:24:32,300 --> 00:24:34,900 While executing you are printing 'each' also. That's why we're getting 459 00:24:34,900 --> 00:24:40,400 5. Then again your Python will try to look for next value 460 00:24:40,400 --> 00:24:43,500 in the given list, but you don't have next value. Already 461 00:24:43,600 --> 00:24:44,700 four values are over. 462 00:24:45,100 --> 00:24:48,500 That's why your Python will come out from your loop and will 463 00:24:48,500 --> 00:24:51,800 execute the next line, which is the main line in your Python 464 00:24:51,800 --> 00:24:55,900 code. If four values are over then the execution part of this 465 00:24:55,900 --> 00:24:58,800 line is over. Then your Python is going to execute this 466 00:24:58,800 --> 00:25:00,200 one. That's why you're getting this line. 467 00:25:00,200 --> 00:25:02,600 [no audio] 468 00:25:02,600 --> 00:25:06,900 Right. So now if you have a list of values 469 00:25:08,100 --> 00:25:10,200 from that automatically 470 00:25:10,200 --> 00:25:14,000 you can take one by one value and you can print it using your loop. 471 00:25:14,900 --> 00:25:18,800 Now this concept I am going to use for our task. How means, 472 00:25:19,300 --> 00:25:23,300 how means, instead of writing this logic, see first of all 473 00:25:23,300 --> 00:25:24,500 let me copy this code. 474 00:25:25,500 --> 00:25:30,300 So previously you are taking only 'p1' and 'p2', only two paths 475 00:25:30,300 --> 00:25:32,800 you're taking but you know in the given path you may have 476 00:25:32,800 --> 00:25:34,200 any number of paths, 477 00:25:34,200 --> 00:25:35,600 I mean files and directories. 478 00:25:36,000 --> 00:25:37,200 That's why don't fix. 479 00:25:37,700 --> 00:25:40,310 You don't know about that path, in that path 480 00:25:40,300 --> 00:25:42,300 you don't know how many files are there, 481 00:25:42,300 --> 00:25:43,500 how many directories are there. 482 00:25:43,700 --> 00:25:46,000 How can you say only two paths, 'p1' and 'p2'? 483 00:25:47,100 --> 00:25:50,400 That's why what I am doing is, right, 484 00:25:50,600 --> 00:25:52,200 let me comment this logic as well. 485 00:25:52,500 --> 00:25:53,700 This is simple for loop guys. 486 00:25:54,300 --> 00:25:55,500 Okay. We have different ways. 487 00:25:55,500 --> 00:25:58,100 We will see all those loops in the next section. 488 00:25:58,100 --> 00:26:00,500 [no audio] 489 00:26:00,500 --> 00:26:02,000 I am importing 'os', then 490 00:26:02,000 --> 00:26:06,100 I am reading some path, after reading path, right, I am getting 491 00:26:06,100 --> 00:26:08,000 a list of files and directories. 492 00:26:08,000 --> 00:26:10,500 [no audio] 493 00:26:10,500 --> 00:26:16,700 Let me take lengthy variable, 'os.listdir', your 494 00:26:16,700 --> 00:26:17,700 given path. 495 00:26:17,900 --> 00:26:21,900 Now, you know, the result of this operation is always a list. 496 00:26:21,900 --> 00:26:25,300 That means this variable is a list. If it is a list I can 497 00:26:25,300 --> 00:26:28,900 take one by one from that using for loop. See that, what I 498 00:26:28,900 --> 00:26:33,300 am doing. 'for each_file', I am taking. See previously I have taken 499 00:26:33,300 --> 00:26:35,800 only 'each' while explaining about your loop, but now I am 500 00:26:35,800 --> 00:26:38,500 taking 'each_file'. Based on situation 501 00:26:38,500 --> 00:26:42,400 you can take any variable. Now, from this variable list of 502 00:26:42,400 --> 00:26:44,200 things I am going to take one by one. 503 00:26:44,400 --> 00:26:47,400 So in that you may have files and directories, but I am taking 504 00:26:47,400 --> 00:26:50,500 'each_file'. Instead of that 'each_file_or_dir' you 505 00:26:50,500 --> 00:26:54,600 can take, but don't take this lengthy, this type of lengthy 506 00:26:54,600 --> 00:26:56,300 variable. Okay. For understanding purpose 507 00:26:56,300 --> 00:26:59,700 I am taking 'each_file_or_dir' from this. 508 00:27:01,400 --> 00:27:07,500 Right. Now you have to understand that how many times this 509 00:27:07,500 --> 00:27:10,700 loop is going to execute this 'print' statement. 510 00:27:12,000 --> 00:27:15,200 You don't know how many times it is going to execute. It is 511 00:27:15,200 --> 00:27:18,400 going to execute based on how many values you are fetching 512 00:27:18,400 --> 00:27:20,700 into this variable. This variable result 513 00:27:20,700 --> 00:27:23,100 you are getting from 'os.listdir'. 514 00:27:23,100 --> 00:27:28,700 That means you are getting these values. Where it is? Where is your 515 00:27:29,500 --> 00:27:31,800 Yeah, this values. 516 00:27:31,800 --> 00:27:35,400 So indirectly you can assume a list of values in place of 517 00:27:35,400 --> 00:27:37,700 this 'list_of_files_dir'. 518 00:27:38,400 --> 00:27:42,200 So what your Python will take? It will take first value from 519 00:27:42,200 --> 00:27:45,000 this and it will assign to this, and then we'll execute the 520 00:27:45,000 --> 00:27:48,600 lines which are there under your loop. So I have only one 521 00:27:48,600 --> 00:27:51,600 'print' statement that will execute. Then your Python will try to 522 00:27:51,600 --> 00:27:55,300 take next item from this file or directory, and it will store 523 00:27:55,300 --> 00:27:59,500 into this variable and we are printing that. That's why dynamically, 524 00:27:59,600 --> 00:28:03,900 automatically you are going to print all your files or directories 525 00:28:03,900 --> 00:28:07,000 from your given location. Now, you don't need to bother, you 526 00:28:07,000 --> 00:28:08,000 don't need to worry, 527 00:28:08,400 --> 00:28:12,100 how many files are there, how many directories are there. Automatically, 528 00:28:12,100 --> 00:28:14,800 your Python is going to take care with the help of your loop. 529 00:28:16,300 --> 00:28:20,000 Right. Now, let me go and run your code. Before that 530 00:28:20,000 --> 00:28:21,300 let me open your code. 531 00:28:21,300 --> 00:28:23,100 [no audio] 532 00:28:23,100 --> 00:28:24,700 See this is your code simply. 533 00:28:25,700 --> 00:28:27,400 I am going to provide some path. 534 00:28:28,000 --> 00:28:31,700 Let me do one thing. I am going to remove all empty lines. 535 00:28:31,700 --> 00:28:35,500 [no audio] 536 00:28:35,500 --> 00:28:37,500 Let me clear our screen once again. 537 00:28:37,500 --> 00:28:39,800 Yeah, this is your script. Right. Now 538 00:28:39,800 --> 00:28:43,600 I am going to run your script, and I'm going to provide a 539 00:28:43,600 --> 00:28:44,800 path called 'new'. Now, 540 00:28:44,800 --> 00:28:49,300 see the output. You are able to display one by one file from 541 00:28:49,300 --> 00:28:54,200 your list of values. If you want to see the list of values 542 00:28:54,200 --> 00:29:00,000 you can print that as well. 'print("all files and dirs")'. 543 00:29:00,000 --> 00:29:02,300 You don't know how many files are there and directories are there, 544 00:29:02,600 --> 00:29:06,400 but that value automatically you are getting with 'os.listdir' 545 00:29:06,400 --> 00:29:12,000 into a variable called 'list_of_files_dir'. 546 00:29:12,000 --> 00:29:13,600 Now see the result. 547 00:29:13,600 --> 00:29:15,500 [no audio] 548 00:29:15,500 --> 00:29:16,800 First of all, you are getting this one. 549 00:29:18,200 --> 00:29:20,500 From this list automatically your Python is going to take 550 00:29:20,500 --> 00:29:23,600 with the help of for loop one by one value, and we are printing 551 00:29:23,600 --> 00:29:24,700 that one by one value. 552 00:29:26,300 --> 00:29:31,100 But my intention is not displaying simply your one by one value. 553 00:29:31,100 --> 00:29:33,600 If it is a directory I want to display here, 554 00:29:33,600 --> 00:29:34,500 "This is a directory". 555 00:29:34,500 --> 00:29:35,700 "This is a file. "This is a file". 556 00:29:35,700 --> 00:29:40,200 "This is a file". But before going to display, before going 557 00:29:40,200 --> 00:29:43,800 to check whether that is a file or directory always, not only 558 00:29:43,800 --> 00:29:45,500 here, wherever you are using 559 00:29:46,800 --> 00:29:49,700 you should able to fetch complete path first. 560 00:29:50,900 --> 00:29:56,500 So that's why what I am doing is, before printing I'm going 561 00:29:56,500 --> 00:29:58,900 to create one variable called 'path'. 562 00:30:00,000 --> 00:30:04,200 Let me take file or directory path, file or directory. 563 00:30:04,200 --> 00:30:06,000 I don't know whether that is a file or path. 564 00:30:07,100 --> 00:30:11,900 Right. See your given, if you add your given path to this 565 00:30:11,900 --> 00:30:13,100 'each_file_or_dir', 566 00:30:13,200 --> 00:30:17,400 so that you are going to get complete path, right. So directly 567 00:30:17,400 --> 00:30:19,400 you can print it, but I am storing into one variable, 568 00:30:19,400 --> 00:30:24,900 then I will, 'print(os.path.join)', your given path, 569 00:30:24,900 --> 00:30:26,600 in that location one by one file 570 00:30:26,600 --> 00:30:29,400 I am taking, and I'm adding that one by one file for your 571 00:30:29,400 --> 00:30:32,100 path. Then I am going to print. 572 00:30:32,100 --> 00:30:35,400 [no audio] 573 00:30:35,400 --> 00:30:37,900 Okay. So guys for your understanding purpose what I am 574 00:30:37,900 --> 00:30:42,900 taking is, this value I am taking. Okay. And I am copying directly 575 00:30:42,900 --> 00:30:44,400 that into your script. 576 00:30:45,200 --> 00:30:48,100 So instead of this 'list_of_files_dir' actually 577 00:30:48,100 --> 00:30:51,600 you are getting that output as in this way. Just for, just to 578 00:30:51,600 --> 00:30:56,000 give you the clarity. Right. Now assume that you're providing 579 00:30:56,000 --> 00:30:57,500 path as something called, 580 00:30:58,600 --> 00:31:01,000 this one you are providing, right? 581 00:31:01,000 --> 00:31:05,100 Let me take directly that also in your script. Again, I will remove this. 582 00:31:05,200 --> 00:31:08,600 Anyway finally we'll run our script. Now forget about your 583 00:31:08,600 --> 00:31:12,500 previous logic. Now see here, 584 00:31:12,800 --> 00:31:14,800 this is a path and in this path 585 00:31:14,800 --> 00:31:15,900 we know, assume that 586 00:31:15,900 --> 00:31:19,300 we know these are the list of values or items. 587 00:31:19,900 --> 00:31:22,100 Now, what I am doing is I am going to take one by one from this 588 00:31:22,300 --> 00:31:25,600 'for each_file_or_dir'. What your Python will do? 589 00:31:26,400 --> 00:31:30,100 This first value it will assign to this variable, and the lines 590 00:31:30,100 --> 00:31:32,800 which are there under your loop, those lines will execute 591 00:31:32,800 --> 00:31:37,400 once. While executing what we are doing? I am taking one variable 592 00:31:37,800 --> 00:31:40,100 and with the help of 'os.path.join', 593 00:31:40,100 --> 00:31:44,200 I'm adding this path, see that, I am adding this path. 594 00:31:44,300 --> 00:31:49,500 Then first time your file is 'mydir'. then 'mydir' I am adding here, 595 00:31:50,900 --> 00:31:53,500 so that I am going to get for this complete path. 596 00:31:54,700 --> 00:31:57,200 Then you are going to print that. Second time you are going 597 00:31:57,200 --> 00:31:58,200 to take this one, 598 00:31:58,300 --> 00:31:59,800 but again you are adding for your path 599 00:31:59,800 --> 00:32:01,400 only. Your path is your given path. 600 00:32:01,800 --> 00:32:05,400 So this path, then this file is going to give you complete 601 00:32:05,400 --> 00:32:11,000 path. Likewise you are going to get path for all the files, 602 00:32:11,300 --> 00:32:14,000 right? So actually what is the variable here we have taken? 603 00:32:14,100 --> 00:32:18,900 Let me. Dynamically you have to to work it. 604 00:32:19,000 --> 00:32:21,400 That's why don't fix any values here. 605 00:32:22,600 --> 00:32:24,900 But internally you have to assume that these values are a list. 606 00:32:25,600 --> 00:32:26,700 Then remove this one. 607 00:32:26,700 --> 00:32:28,800 [no audio] 608 00:32:28,800 --> 00:32:31,200 That is your logic, right? 609 00:32:31,200 --> 00:32:34,200 If you want to check your script just this is your script 610 00:32:34,500 --> 00:32:37,700 from here to here. Now I am running, 611 00:32:39,400 --> 00:32:42,500 and I am providing a path. Now see the output. For each and 612 00:32:42,500 --> 00:32:44,900 every file or directory you're getting complete path. 613 00:32:44,900 --> 00:32:46,900 [no audio] 614 00:32:46,900 --> 00:32:50,900 Right. But simply instead of printing in this way, 615 00:32:50,900 --> 00:32:53,700 if it is a directory I want to print, "This is a directory", 616 00:32:53,700 --> 00:32:57,500 if it is a file I want to print, "This is a file". Now inside 617 00:32:57,500 --> 00:32:58,700 of your loop itself 618 00:32:58,700 --> 00:33:05,300 I am going to add 'if' condition. 'if os.path.isfile', 619 00:33:05,600 --> 00:33:13,300 your file or directory. If it is a file I can say 'f_d_p' 620 00:33:13,700 --> 00:33:14,900 "is a file". 621 00:33:16,400 --> 00:33:18,300 'else' if that is not correct, 622 00:33:18,400 --> 00:33:21,500 that means that is not a file. That is not a file means then 623 00:33:21,500 --> 00:33:26,700 you can say that that is a directory, no. "is a directory". That's 624 00:33:26,700 --> 00:33:31,400 it. Save it and run it. But before going to run you have to 625 00:33:31,400 --> 00:33:33,400 understand once again your entire code. 626 00:33:34,800 --> 00:33:38,600 This entire part you are repeating for each and every value 627 00:33:38,600 --> 00:33:41,000 which is there in a list. List is this one, 628 00:33:41,000 --> 00:33:45,000 no. This is a list. You're taking one by one and for that 629 00:33:45,000 --> 00:33:48,100 one by one you are finding complete path. With that complete 630 00:33:48,100 --> 00:33:53,000 path you are executing this 'if-else' block. 'if os.path.isfile', 631 00:33:53,000 --> 00:33:54,000 for your complete path, 632 00:33:54,000 --> 00:33:57,500 if it is a file this will execute, then your first iteration 633 00:33:57,500 --> 00:33:59,000 is over for first value. 634 00:34:00,100 --> 00:34:02,700 Then you will take second value. For second value you are going 635 00:34:02,700 --> 00:34:03,800 to get complete path. 636 00:34:03,900 --> 00:34:04,100 Again. 637 00:34:04,100 --> 00:34:07,400 Again you are checking 'if-else' condition. In case if 'isfile' 638 00:34:07,400 --> 00:34:09,100 is a 'False', then you are printing this one. 639 00:34:09,699 --> 00:34:11,800 Then second iteration is also over. Likewise 640 00:34:12,100 --> 00:34:17,000 your for loop is doing this action for each and every file 641 00:34:17,100 --> 00:34:20,500 so that finally you are going to get dynamically, for any 642 00:34:20,500 --> 00:34:24,300 number of files and directories you are able to identify 643 00:34:24,400 --> 00:34:29,000 the list of files and directories. See the output. "is a directory" 644 00:34:29,000 --> 00:34:31,199 you're getting. "is a file" you are. Not only this path, 645 00:34:31,199 --> 00:34:33,000 now you can provide any path. 646 00:34:33,400 --> 00:34:34,900 Let me take some other path. 647 00:34:34,900 --> 00:34:37,100 I am going to take suppose this as my path. 648 00:34:37,100 --> 00:34:40,800 [no audio] 649 00:34:40,800 --> 00:34:43,699 Run. Provide your path. Guys 650 00:34:43,699 --> 00:34:44,500 this is Windows, 651 00:34:44,500 --> 00:34:46,900 that's why I am providing '\\'. Same script 652 00:34:46,900 --> 00:34:48,900 you can run on your Unix-like systems as well, 653 00:34:49,600 --> 00:34:53,500 it will work without any error. In case if you are getting any error, 654 00:34:54,300 --> 00:34:55,199 anyway you won't get it. 655 00:34:55,699 --> 00:34:58,400 If you have any challenges, just let me know so that I will 656 00:34:58,400 --> 00:35:01,900 give the complete script to work with your Unix as well. 657 00:35:02,300 --> 00:35:04,600 But without any doubt it will work. See the result. 658 00:35:05,600 --> 00:35:09,199 In the given location, first of all you're having this many 659 00:35:09,199 --> 00:35:12,000 number of files, maybe directories or files. 660 00:35:12,000 --> 00:35:14,400 You don't know which is a file and which is a directory, assume 661 00:35:14,400 --> 00:35:19,100 that. Then your Python is able to form complete path. The path 662 00:35:19,100 --> 00:35:22,900 whatever we are giving, from that your Python is able to add 663 00:35:22,900 --> 00:35:25,500 this path. Then it is giving - "is a file", "is a file", "is a file", 664 00:35:25,500 --> 00:35:27,900 "is a file", "is a file", "is a file", "is a file". 665 00:35:27,900 --> 00:35:28,699 That's what it is working. 666 00:35:28,699 --> 00:35:31,800 [no audio] 667 00:35:31,800 --> 00:35:32,800 Right. 668 00:35:34,000 --> 00:35:35,000 That's it. 669 00:35:36,100 --> 00:35:41,400 So guys this entire explanation is just to understand your 670 00:35:41,400 --> 00:35:42,600 loop concept. 671 00:35:43,500 --> 00:35:48,300 Okay. See I have taken this example but my intention is not 672 00:35:48,300 --> 00:35:51,200 to develop this script, to give your loop concepts. 673 00:35:51,800 --> 00:35:55,700 So here I have given over all idea. In later sections 674 00:35:55,800 --> 00:35:59,100 I will give step-by-step again your complete loop how to use 675 00:35:59,100 --> 00:36:01,600 that, what is the complete syntax, in which scenario you are 676 00:36:01,600 --> 00:36:03,500 going to use which loop, all those things 677 00:36:03,500 --> 00:36:06,600 we will see. We have two Loops. One is for loop. 678 00:36:06,600 --> 00:36:10,300 We have seen that. Next Loop is while loop. Along with that 679 00:36:10,600 --> 00:36:13,200 to control your loop actions you have 680 00:36:13,200 --> 00:36:18,200 some 'break' and 'continue' keywords. 'break' and 'continue'. 681 00:36:19,100 --> 00:36:23,500 So this also I am going to explain with the help of examples 682 00:36:23,500 --> 00:36:27,700 only. I won't go directly, and I won't give your syntax, and 683 00:36:27,700 --> 00:36:30,500 I won't write your code directly by using your loops. 684 00:36:31,000 --> 00:36:34,000 I will take a situation, then so that you can easily understand 685 00:36:34,400 --> 00:36:37,600 what is the purpose of that, why we need it, right? 686 00:36:38,400 --> 00:36:39,600 Okay. Okay guys, 687 00:36:39,700 --> 00:36:41,000 thank you for watching this video. 688 00:36:41,000 --> 00:36:51,520 [no audio]