1 00:00:00,000 --> 00:00:02,600 [no audio] 2 00:00:02,600 --> 00:00:04,200 Friends here we are going to do 3 00:00:04,200 --> 00:00:06,800 a small practice with your 'os.walk' 4 00:00:06,800 --> 00:00:11,900 function and that is, 'How to do a system-wide search 5 00:00:11,900 --> 00:00:15,700 for a given file?' Assume that in your hand you have one file, 6 00:00:15,900 --> 00:00:19,600 but you want to search that file path in your entire file 7 00:00:19,600 --> 00:00:23,700 system, where it is located, that path I need to find out. 8 00:00:25,100 --> 00:00:28,200 And one more thing while developing this script you have 9 00:00:28,200 --> 00:00:31,600 to develop this script such a way that it should be like 10 00:00:31,600 --> 00:00:33,500 platform independent, 11 00:00:33,500 --> 00:00:37,800 that means same script should work with your 12 00:00:37,800 --> 00:00:41,800 Windows, and as well as your Unix-like systems. For time being 13 00:00:41,800 --> 00:00:45,800 first I am going to develop this script on your Unix-like 14 00:00:45,800 --> 00:00:50,100 systems, and again we will develop a code on your Windows as well. 15 00:00:50,500 --> 00:00:54,000 First of all, let me open my Unix-like system. 16 00:00:54,500 --> 00:00:56,000 So this is my Unix-like system. 17 00:00:56,900 --> 00:01:01,100 So guys, if you know the file system about your Unix-like 18 00:01:01,100 --> 00:01:06,500 system, suppose if I give simply a root, this symbol, '/' then 19 00:01:07,200 --> 00:01:10,400 this is nothing but your entire file system on your Unix-like system. 20 00:01:10,400 --> 00:01:13,300 Now, if you take this as a path by default the meaning for 21 00:01:13,300 --> 00:01:16,900 that is, you are going to look for entire system in your Unix-like 22 00:01:16,900 --> 00:01:20,000 systems. Let me take one simple example. 23 00:01:20,000 --> 00:01:22,500 I want to know where my, 24 00:01:22,500 --> 00:01:24,800 [no audio] 25 00:01:24,800 --> 00:01:29,200 something like 'host.conf' file is there. 26 00:01:30,200 --> 00:01:32,300 See you are getting some 'Permission denied', because 27 00:01:32,300 --> 00:01:35,200 root, you are giving entire file system means, you should 28 00:01:35,200 --> 00:01:36,200 have root privileges. 29 00:01:36,800 --> 00:01:39,900 So for time being just I am showing with your Unix command 30 00:01:39,900 --> 00:01:41,900 lines, Unix commands. 31 00:01:42,400 --> 00:01:46,100 Yes, if you have a proper permission, then you are getting exact 32 00:01:46,100 --> 00:01:49,800 location where your file is there. Right. This thing 33 00:01:49,800 --> 00:01:55,100 we are going to perform with your Python, and for time being, 34 00:01:55,200 --> 00:01:58,400 right, here just to implement this script, for time being I 35 00:01:58,400 --> 00:01:59,700 am entering into my root. 36 00:01:59,700 --> 00:02:01,700 [no audio] 37 00:02:01,700 --> 00:02:04,900 Now from here, I am going to develop my Python script just 38 00:02:04,900 --> 00:02:08,900 to identify any file based on your requirement, right. 39 00:02:08,900 --> 00:02:12,800 So what I am doing is, I am implementing a Python script, right 40 00:02:13,500 --> 00:02:14,500 'search_a_file'. 41 00:02:16,000 --> 00:02:17,000 in your entire system. 42 00:02:18,700 --> 00:02:22,100 Fine. First of all, I am going to read the file name. What is the file 43 00:02:22,100 --> 00:02:25,700 you want to search in your entire system, in entire file system, 44 00:02:26,100 --> 00:02:31,800 right? Let me take 'req_file=', I want to read 45 00:02:31,800 --> 00:02:34,600 a file name, only file name, right. 46 00:02:34,700 --> 00:02:39,000 So, "Enter your file name to search: ". 47 00:02:39,000 --> 00:02:42,200 [no audio] 48 00:02:42,200 --> 00:02:44,300 Assume that user is going to enter your file. 49 00:02:45,100 --> 00:02:50,000 Right. So guys, if you remember from our previous video, 50 00:02:50,000 --> 00:02:55,200 if you have a path, then automatically you are able to find 51 00:02:55,200 --> 00:02:58,800 out all the file names which are there in your operating system. 52 00:03:00,500 --> 00:03:01,300 Right. See that. 53 00:03:02,100 --> 00:03:06,000 First of all what I will do is, I will take for simplicity purpose 54 00:03:06,000 --> 00:03:08,000 [no audio] 55 00:03:08,000 --> 00:03:09,000 I will do one thing. 56 00:03:10,300 --> 00:03:14,000 So this path I will take, for simplicity purpose first. Then 57 00:03:14,000 --> 00:03:15,600 I will take root file system. 58 00:03:16,700 --> 00:03:18,700 See using your 'os.walk', 59 00:03:19,900 --> 00:03:22,100 right, that's why I'm importing our 'os'. 60 00:03:23,400 --> 00:03:25,700 'for each', no, no. 61 00:03:25,700 --> 00:03:27,200 Right. In your 'os.walk' 62 00:03:27,200 --> 00:03:31,600 you're going to get three things. Root directory, directories 63 00:03:31,600 --> 00:03:35,900 list, files list, 'r,d,f', in our 'os.walk' whatever the path you're 64 00:03:35,900 --> 00:03:37,700 going to provide. For time being directly 65 00:03:37,700 --> 00:03:40,000 I'm giving this path. 66 00:03:41,400 --> 00:03:44,600 Then I am going to print your root 67 00:03:44,600 --> 00:03:45,800 directory, then files. 68 00:03:46,700 --> 00:03:49,400 So because of this you are going to get your root directory, 69 00:03:49,400 --> 00:03:52,100 and the files which are there in that location. But I want 70 00:03:52,100 --> 00:03:55,900 to print, I want to print each and every file with complete 71 00:03:55,900 --> 00:04:02,200 path. Right. See that, what I am doing. 'for each_file in f'. Guys 72 00:04:02,200 --> 00:04:06,400 if you are not able to understand this step, please go to 73 00:04:06,400 --> 00:04:10,500 your previous video, and watch that video once again, then come back. 74 00:04:12,100 --> 00:04:15,600 Then I am printing simply 'os.path.join()', 75 00:04:15,600 --> 00:04:18,800 [no audio] 76 00:04:18,899 --> 00:04:24,399 your root, then your 'each_file'. Suppose if I save this and 77 00:04:24,399 --> 00:04:27,399 if I run this, see the script how it is going to work. 78 00:04:27,399 --> 00:04:29,600 [no audio] 79 00:04:29,600 --> 00:04:32,900 "Enter your filename to search: ". Anyway for that 80 00:04:32,900 --> 00:04:34,300 we've not yet implemented any logic, 81 00:04:34,300 --> 00:04:36,000 let me take simply 'test.py'. 82 00:04:37,800 --> 00:04:40,800 See from your '/home/ec2-user' 83 00:04:40,800 --> 00:04:42,200 automatically as of now 84 00:04:42,200 --> 00:04:46,900 your Python is able to create all files, all files with complete 85 00:04:46,900 --> 00:04:51,500 path. Somewhere you have your 'test.py', right? 86 00:04:51,500 --> 00:04:52,500 Let me show you that. 87 00:04:52,500 --> 00:04:57,100 [no audio] 88 00:04:57,100 --> 00:04:58,200 Right. Yeah. 89 00:04:59,500 --> 00:05:00,900 See, you're getting somewhere. 90 00:05:00,900 --> 00:05:04,300 I want to print only this file because I am giving my searchable 91 00:05:04,300 --> 00:05:09,500 file is 'test.py'. So, what I will do is very simple guys, 'vi'. 92 00:05:09,500 --> 00:05:12,600 [no audio] 93 00:05:12,600 --> 00:05:18,200 Before printing, if file is, see that, what I'm doing. 'if each_file', 94 00:05:18,200 --> 00:05:22,200 if it is exactly equal to your 'req_file', then 95 00:05:22,200 --> 00:05:23,400 only I am printing that path, 96 00:05:24,800 --> 00:05:26,400 complete path I am printing. That's it. 97 00:05:26,400 --> 00:05:33,300 [no audio] 98 00:05:33,300 --> 00:05:36,700 'test.py'. Yes, you are getting complete path where it is 99 00:05:36,700 --> 00:05:38,700 located. Not only that. 100 00:05:38,700 --> 00:05:42,500 Let me take some other files which are having, let me first 101 00:05:42,500 --> 00:05:46,500 display the files which I'm having in my, this location. 102 00:05:46,500 --> 00:05:49,600 [no audio] 103 00:05:49,600 --> 00:05:51,000 Let me take 'hi.txt', 104 00:05:51,400 --> 00:05:54,400 and let me run our Python script, and let me provide the search, 105 00:05:55,100 --> 00:05:58,400 your required file to search. You are getting complete path. 106 00:05:59,200 --> 00:06:03,800 Now, as of now in your script whatever you have, 'os.walk', 107 00:06:03,800 --> 00:06:08,200 that is taking path as some user 'home', but I want to 108 00:06:08,200 --> 00:06:11,500 search an entire system, entire file system of your Unix-like 109 00:06:11,500 --> 00:06:14,700 systems. Then I can take simply '/'. That's it. 110 00:06:15,900 --> 00:06:20,400 Now, let me run it, and let me run and provide a filename 111 00:06:20,400 --> 00:06:22,500 called 'test.py', you can take anything. 112 00:06:23,600 --> 00:06:26,000 See, in this many places you have 'test.py'. 113 00:06:26,000 --> 00:06:28,100 [no audio] 114 00:06:28,100 --> 00:06:32,400 Now, let me run, and let me provide 'host.conf'. 115 00:06:33,600 --> 00:06:38,400 Let me run, and let me provide something called, what I can say 'pip'. 116 00:06:39,800 --> 00:06:41,300 This many places you have 'pip'. 117 00:06:42,600 --> 00:06:43,900 Let me provide 'pip3'. 118 00:06:44,900 --> 00:06:47,700 Not only that, anything you can get. Right. Now, 119 00:06:47,700 --> 00:06:53,000 we implemented a simple Python script to search your file in 120 00:06:53,000 --> 00:06:54,300 your entire file system. 121 00:06:55,400 --> 00:06:56,400 So this is for your 122 00:06:56,400 --> 00:06:58,400 [no audio] 123 00:06:58,400 --> 00:07:01,100 Unix-like systems. Let's come back with 124 00:07:01,100 --> 00:07:03,400 your Windows. Now here 125 00:07:03,400 --> 00:07:07,900 I am going to implement for your Windows. Then finally 126 00:07:07,900 --> 00:07:10,200 I will club those two codes. Okay. 127 00:07:11,000 --> 00:07:12,800 Let me open my Sublime Text first. 128 00:07:12,800 --> 00:07:16,200 [no audio] 129 00:07:16,200 --> 00:07:20,600 Guys, if you observe, in your Windows operating system you 130 00:07:20,600 --> 00:07:22,900 have drives. Based on drives 131 00:07:22,900 --> 00:07:24,400 you have different partitions. 132 00:07:25,300 --> 00:07:29,500 So that means there is no single symbol to identify your 133 00:07:29,500 --> 00:07:31,900 entire file systems on your Windows operating system. 134 00:07:32,300 --> 00:07:38,000 See, if you go with your Unix-like systems you have this '/' symbol. 135 00:07:38,000 --> 00:07:40,600 So that '/' means your entire file system. 136 00:07:41,100 --> 00:07:45,500 So this type of symbol is not there with your Windows operating 137 00:07:45,500 --> 00:07:49,300 systems, right. Then based on drive 138 00:07:49,300 --> 00:07:50,300 we need to search. 139 00:07:51,800 --> 00:07:56,000 Based on drive we need to search our file in your Windows operating 140 00:07:56,000 --> 00:08:02,300 system. But I don't know how many drives are there. Right. See, first 141 00:08:02,300 --> 00:08:04,400 let me implement the code for one drive. 142 00:08:04,600 --> 00:08:07,700 Okay. Let me 'import os'. Let me save it first, 143 00:08:07,700 --> 00:08:14,300 [no audio] 144 00:08:14,300 --> 00:08:15,400 'search_a_file'. 145 00:08:16,800 --> 00:08:19,800 '.py'. Right. Okay. 146 00:08:19,800 --> 00:08:25,100 Let me take 'req_file=', same thing, 'input("Enter 147 00:08:25,500 --> 00:08:27,800 your file name to search: ")'. 148 00:08:27,800 --> 00:08:29,700 [no audio] 149 00:08:29,700 --> 00:08:32,799 That's it. Then path. 150 00:08:33,400 --> 00:08:36,500 See guys, what I am saying is, let me do it 151 00:08:36,500 --> 00:08:42,600 first. 'r, d, f in os.walk'. Here 152 00:08:42,600 --> 00:08:46,000 I can't give any symbol like '/'. In Unix-like systems '/' 153 00:08:46,000 --> 00:08:48,400 means it is going to work with your entire file system. 154 00:08:48,400 --> 00:08:51,700 But whereas in Windows based on drives we need to search 155 00:08:51,700 --> 00:08:52,700 our file systems. 156 00:08:52,700 --> 00:08:55,799 So if you have a 'C Drive', then you have to provide 'C Drive' 157 00:08:55,799 --> 00:08:57,000 name, and then you need to search it. 158 00:08:57,700 --> 00:09:01,700 Right. Let's say, let's say I'm going to search from 'C Drive'. 159 00:09:02,800 --> 00:09:07,000 Now what I am doing is, let me take again 'for each_file 160 00:09:08,500 --> 00:09:10,700 in f', right. 161 00:09:11,200 --> 00:09:15,100 'if each_file', if it is '=req_file', 162 00:09:15,500 --> 00:09:17,700 then I can print complete path for that. 163 00:09:19,100 --> 00:09:20,300 Right. 'os. 164 00:09:21,500 --> 00:09:22,800 path.join()', 165 00:09:22,800 --> 00:09:25,200 [no audio] 166 00:09:25,200 --> 00:09:28,900 then your root in that 'each_file'. 167 00:09:30,400 --> 00:09:33,300 Let me go to my command line, and let me run this script. 168 00:09:34,100 --> 00:09:36,800 Let me open my command line. 169 00:09:38,000 --> 00:09:41,300 So under 'Desktop', you have 'PythonScripts' directory. Now, 170 00:09:41,300 --> 00:09:45,400 here you have, let me write 'python search_a_file.py'. 171 00:09:45,400 --> 00:09:48,000 Yeah, this is the script. Now, 172 00:09:48,000 --> 00:09:50,400 I am going to provide a file name called, randomly 173 00:09:50,400 --> 00:09:54,200 I'm going to give it. I can provide anything. For time being 174 00:09:54,200 --> 00:09:55,200 what I am giving is, 175 00:09:55,200 --> 00:09:57,600 [no audio] 176 00:09:57,600 --> 00:10:03,700 let me take simply 'even_od.py'. ''even_od.py' 177 00:10:04,900 --> 00:10:08,100 So it's going to search. Because you have number of file systems 178 00:10:08,100 --> 00:10:09,400 in your Windows 'C Drive', 179 00:10:09,400 --> 00:10:10,800 that's why it's going to take some time. 180 00:10:12,200 --> 00:10:15,300 But anyway, finally you are going to search only in 'C Drive'. 181 00:10:16,100 --> 00:10:17,300 See on my system 182 00:10:17,300 --> 00:10:19,800 I know suppose 'C Drive' is there, but in your operating systems, 183 00:10:19,800 --> 00:10:20,800 you may not have 'C Drive'. 184 00:10:20,800 --> 00:10:24,900 You may have drive name as some other name like 'D' or 'E' or 'F'. 185 00:10:24,900 --> 00:10:26,800 [no audio] 186 00:10:26,800 --> 00:10:30,200 Right. So guys because it's taking some time 187 00:10:30,400 --> 00:10:35,500 what I will do is, I will provide a path like at least 'Desktop' 188 00:10:35,500 --> 00:10:41,200 path, so that it will be easy for our script to identify quickly. 189 00:10:42,800 --> 00:10:46,800 Let me provide path as 'Desktop' path, at least up to 'Desktop'. 190 00:10:46,800 --> 00:10:49,400 [no audio] 191 00:10:49,400 --> 00:10:51,400 You know whenever if you're providing a path manually on 192 00:10:51,400 --> 00:10:54,000 your Windows operating systems, you have to provide '\\'. 193 00:10:54,400 --> 00:10:55,400 That's what I am providing. 194 00:10:56,300 --> 00:10:59,300 Now, let me rerun your script and provide the name. 195 00:10:59,700 --> 00:11:00,700 See you're getting. 196 00:11:00,700 --> 00:11:03,100 [no audio] 197 00:11:03,100 --> 00:11:05,500 That's fine guys. But what is the problem 198 00:11:05,500 --> 00:11:07,800 with your Windows operating systems is, manually you're 199 00:11:07,800 --> 00:11:09,200 providing 'C Drive' path. 200 00:11:10,300 --> 00:11:13,600 If you go with any Unix-like systems, complete file system is '/'. 201 00:11:13,600 --> 00:11:15,600 [no audio] 202 00:11:15,600 --> 00:11:20,600 Now I want to search an entire file system in your Windows. 203 00:11:21,200 --> 00:11:24,400 There is no single symbol to identify that, then how you 204 00:11:24,400 --> 00:11:28,300 can identify all your drives on your operating system. 205 00:11:29,900 --> 00:11:31,100 Right. Nothing 206 00:11:31,100 --> 00:11:34,400 is there guys. Before running, before searching any file on 207 00:11:34,400 --> 00:11:37,200 your Windows operating system, logically if you want to 208 00:11:37,200 --> 00:11:40,300 identify all the drives in your Windows operating 209 00:11:40,300 --> 00:11:43,500 systems you need to write some extra logic whenever you 210 00:11:43,500 --> 00:11:44,600 are working with your windows. 211 00:11:45,300 --> 00:11:47,700 Let me take possible drives 212 00:11:47,700 --> 00:11:50,500 generally. If you go with the possible drives they are from 213 00:11:50,500 --> 00:11:53,800 suppose some 'a' to 'z', small letters and capital letters, right? 214 00:11:54,500 --> 00:11:55,700 Let me take it in this way. 215 00:11:55,700 --> 00:11:57,600 [no audio] 216 00:11:57,600 --> 00:11:59,900 Right. But I don't want to write. I'm a lazy person. 217 00:11:59,900 --> 00:12:03,700 I don't want to write a, b, c, d, small and capitals. Then what 218 00:12:03,700 --> 00:12:06,900 I am doing is, I am entering into my Python terminal. Just observe here. 219 00:12:06,900 --> 00:12:10,600 There is a default module called 'string', and if you observe 220 00:12:10,600 --> 00:12:14,200 the operations with your 'string', there is something called 221 00:12:14,200 --> 00:12:15,300 'ascii_letters'. 222 00:12:16,700 --> 00:12:20,300 Right. Nothing is there. Simply 'print(string(), 223 00:12:21,100 --> 00:12:26,600 that is the module, '.ascii_letters'. 224 00:12:26,600 --> 00:12:29,800 [no audio] 225 00:12:29,800 --> 00:12:32,200 Sorry. See the result. 226 00:12:33,200 --> 00:12:36,000 Small letters and capital letters directly you are getting. I don't 227 00:12:36,000 --> 00:12:37,100 want to type this one. 228 00:12:37,500 --> 00:12:41,100 So nothing is there. Just 'import string' module. You have 229 00:12:41,100 --> 00:12:42,600 so many things here, just observe them. 230 00:12:43,800 --> 00:12:46,900 Now what I am doing is, I am also importing 'string' module. 231 00:12:48,100 --> 00:12:55,500 Then possible drives names I am taking. 'pd_name=', 232 00:12:55,500 --> 00:12:57,200 simply I can write in this way. Now, 233 00:12:57,200 --> 00:12:58,400 let me print first, 234 00:13:00,000 --> 00:13:02,000 Guys just observe that. Finally you can understand 235 00:13:02,000 --> 00:13:03,300 what is the usage of this thing. 236 00:13:04,700 --> 00:13:07,000 Now what I will do is, I will take one by one 237 00:13:08,400 --> 00:13:10,800 letters from your 'pd_names', 238 00:13:10,800 --> 00:13:13,400 I mean I'm taking one by one possible drive name. 239 00:13:13,400 --> 00:13:16,600 [no audio] 240 00:13:16,600 --> 00:13:17,900 Right. See. 241 00:13:17,900 --> 00:13:20,300 [no audio] 242 00:13:20,300 --> 00:13:21,300 One thing. 243 00:13:22,200 --> 00:13:26,700 Let me take 'each_drive', each drive name 244 00:13:28,100 --> 00:13:31,000 in your 'pd_names'. That is a 'string'. 245 00:13:31,300 --> 00:13:34,400 So if it is a string I can take one by one character separately 246 00:13:34,400 --> 00:13:35,900 with the help of your for loop. 247 00:13:37,600 --> 00:13:39,600 Yes. You're getting one by one, right? 248 00:13:40,500 --> 00:13:43,700 But now what I am doing is, see I don't have all these drives 249 00:13:43,700 --> 00:13:45,200 on my operating system, right? 250 00:13:45,200 --> 00:13:48,400 I have only some of them. But I need to identify which drive 251 00:13:48,400 --> 00:13:51,300 you have in our operating system, from A to Z. 252 00:13:52,800 --> 00:13:56,100 Right. So that's why what I am writing is, I am checking. 253 00:13:57,600 --> 00:14:08,400 See 'if os.path.exist(each_drive)', before that 254 00:14:08,400 --> 00:14:12,500 I want to, yeah, let me print. Because if it is 'C drive', right, 255 00:14:12,500 --> 00:14:17,500 if it is a 'C Drive' how your path would be? 'c:\'. 256 00:14:18,400 --> 00:14:20,400 Right. So now I need to form that 257 00:14:20,500 --> 00:14:24,200 first of all. If it is a path then how it would be. 258 00:14:25,400 --> 00:14:29,100 Where is your code? Yeah. 'each_drive +', 259 00:14:30,100 --> 00:14:31,900 ':', so manually you are writing, 260 00:14:31,900 --> 00:14:35,100 it's better to write '\\'. Now see the output. 261 00:14:36,400 --> 00:14:39,300 You're getting in this way your paths. But whether this is 262 00:14:39,300 --> 00:14:42,100 a valid drive on your host or not 263 00:14:42,100 --> 00:14:42,900 you have to check it. 264 00:14:43,200 --> 00:14:45,000 This is like a path, right? 265 00:14:45,100 --> 00:14:48,600 So what I am doing is, just simply I am applying 'if' condition, 266 00:14:48,600 --> 00:14:52,600 'if os.path.exist', 267 00:14:53,300 --> 00:14:55,200 if this is a valid path on your host 268 00:14:55,200 --> 00:14:58,100 this condition will become 'True', then only I want to print 269 00:14:58,100 --> 00:14:59,100 the drive name, 270 00:14:59,100 --> 00:15:01,000 [no audio] 271 00:15:01,000 --> 00:15:04,700 then only I want to print the drive name. Now see, as of now 272 00:15:04,700 --> 00:15:07,200 your output is, all the letters you are getting, I mean all 273 00:15:07,200 --> 00:15:10,000 the drives names you're getting but I'm checking the condition, 274 00:15:10,000 --> 00:15:13,000 if that drive is there on my host then only print that. 275 00:15:13,000 --> 00:15:14,800 [no audio] 276 00:15:14,800 --> 00:15:19,200 Now see that. You're having only 'c', 'e', 'f', and 'C', 'E', 'F'. That means capital 277 00:15:19,200 --> 00:15:23,300 and small drives names are same, there is no problem with that. 278 00:15:23,400 --> 00:15:24,800 That's why, now 279 00:15:24,800 --> 00:15:27,900 if you observe in output, I mean in the 'string' module you 280 00:15:27,900 --> 00:15:30,800 have only either 'ascii_lower' or 'ascii_upper'. 281 00:15:30,800 --> 00:15:34,800 You can take anyone of this. No need to take 'ascii_letters' now. 282 00:15:34,800 --> 00:15:40,000 So I am going to take 'ascii_lowercase', 283 00:15:40,000 --> 00:15:42,100 or if you want to take you can take 'ascii_uppercase'. 284 00:15:42,100 --> 00:15:44,300 You have uppercase as well if you observe here. 285 00:15:45,300 --> 00:15:47,900 Right. Anyone of that path is fine. 286 00:15:47,900 --> 00:15:51,500 [no audio] 287 00:15:51,500 --> 00:15:53,300 Right. Now see the output. 288 00:15:54,300 --> 00:15:58,300 'C', 'E', 'F'. Now if I open my drive, my Windows, 289 00:15:58,300 --> 00:16:01,100 see valid drives are 'C', 'E', and 'F'. 290 00:16:02,200 --> 00:16:04,200 This is empty, right, that's why it is not there as of now. 291 00:16:04,200 --> 00:16:08,100 That's why it is not able to find out G drive. 'C', 'E', 'F' 292 00:16:08,100 --> 00:16:10,100 [no audio] 293 00:16:10,100 --> 00:16:13,500 Now logically, I am able to find all my valid drives 294 00:16:13,500 --> 00:16:14,800 on my Windows operating system. 295 00:16:14,800 --> 00:16:18,400 Now if you run this part on your operating system, I mean 296 00:16:18,500 --> 00:16:19,400 on your Windows 297 00:16:19,500 --> 00:16:22,800 if you run this Python script, you will get based on your 298 00:16:22,800 --> 00:16:24,000 Windows operating system 299 00:16:24,200 --> 00:16:27,700 what are all your drives, that drives names it will display. 300 00:16:28,900 --> 00:16:34,500 Right. Now I am taking a valid drives names. 301 00:16:34,500 --> 00:16:38,100 Let me take simply 'vd_names', valid drives names as a empty 302 00:16:38,100 --> 00:16:42,300 list. If it is a valid one then what I am doing is, I am collecting 303 00:16:42,300 --> 00:16:43,400 that into one variable, 304 00:16:43,800 --> 00:16:48,600 I mean as a list. 'append.each_drive', I am going to create 305 00:16:48,600 --> 00:16:50,600 a complete path for each and every drive. 306 00:16:50,600 --> 00:16:52,600 [no audio] 307 00:16:52,600 --> 00:16:55,800 Right. After completion of your for loop finally just I am 308 00:16:55,800 --> 00:17:00,600 printing. 'print(vd_names)'. 309 00:17:00,600 --> 00:17:03,700 Let me take simply 'names' instead of 'name'. That's better. 310 00:17:04,900 --> 00:17:07,000 Now see the output. Finally you are getting a list, 311 00:17:07,400 --> 00:17:09,500 oh man, names. 312 00:17:09,900 --> 00:17:12,000 Yeah. Now see the output. 313 00:17:13,000 --> 00:17:16,200 These are all valid drives names on my operating system. Now, 314 00:17:16,200 --> 00:17:19,700 actually my intention is I want to search about my file in 315 00:17:19,700 --> 00:17:23,500 entire file system on Windows, right? 316 00:17:23,500 --> 00:17:25,900 That's why first of all I need to identify all of the valid 317 00:17:25,900 --> 00:17:28,098 drives on your Windows operating system. 318 00:17:28,098 --> 00:17:29,000 Now I got them. 319 00:17:29,800 --> 00:17:32,200 Now I don't want to print separately your each and every drive. 320 00:17:32,599 --> 00:17:35,099 I collected all your valid drives as a list. 321 00:17:35,200 --> 00:17:36,900 Yes, I have in my hand. 322 00:17:37,599 --> 00:17:41,200 Now, what I will do is, guys be clear. Here 323 00:17:41,200 --> 00:17:42,300 you may get confused. 324 00:17:42,500 --> 00:17:45,900 What I am doing is, anyway I need to read required file, 325 00:17:45,900 --> 00:17:47,000 right? Okay, 326 00:17:47,000 --> 00:17:48,000 let me do it somewhere. 327 00:17:48,300 --> 00:17:49,800 I want to read 'req_file'. 328 00:17:49,800 --> 00:17:53,599 Yes, I will read. Now instead of this logic, 329 00:17:54,600 --> 00:17:58,600 first of all I need to search in each and every drive, right? 330 00:17:58,600 --> 00:18:02,600 First of all, I am collecting 'each_drive', 'each_drive' in your 331 00:18:02,600 --> 00:18:06,200 'vd_names', and then suppose I am printing your 332 00:18:06,300 --> 00:18:08,600 'each_drive'. See the output. 333 00:18:09,900 --> 00:18:14,900 Okay, let me comment as of now this providing required path. 334 00:18:14,900 --> 00:18:17,600 [no audio] 335 00:18:17,600 --> 00:18:19,000 Yes. Right. 336 00:18:19,300 --> 00:18:20,900 Now, let me comment this line as well. 337 00:18:21,300 --> 00:18:22,400 So using your for loop 338 00:18:22,400 --> 00:18:26,100 first of all you're able to get your drives one by one, 'vd_names'. 339 00:18:26,100 --> 00:18:29,700 Now in each drive you have to search. So that 340 00:18:29,700 --> 00:18:34,700 you can complete for all drives and in your entire file system 341 00:18:34,700 --> 00:18:35,700 you are going to search. 342 00:18:36,500 --> 00:18:40,600 I mean now you have to apply your 'os.walk' for each and every drive. 343 00:18:41,600 --> 00:18:47,200 Nothing is there. See that. 'for r, d, f 344 00:18:47,200 --> 00:18:49,700 in os.walk(each_drive)'. 345 00:18:50,800 --> 00:18:54,200 So now first for loop is just to collect one by 346 00:18:54,200 --> 00:18:59,000 one your 'vd_names'. Then this loop is just to execute 347 00:18:59,000 --> 00:19:01,200 your 'os.walk' on each and every drive. 348 00:19:02,000 --> 00:19:04,200 That's it. Now, let me do it, 'print'. 349 00:19:05,400 --> 00:19:07,000 Yeah, you have list of files, right? 350 00:19:07,000 --> 00:19:09,600 So 'for each_file in each_drive', 351 00:19:10,700 --> 00:19:15,800 right, 'print(os.path.join(r,f))'. 352 00:19:16,300 --> 00:19:21,300 Right. Now if I run this, just observe the output. Because of 353 00:19:21,300 --> 00:19:26,200 this you are going to get all your 'vd_names', and 354 00:19:26,200 --> 00:19:29,600 then in each and every drive directly you're going to print 355 00:19:29,600 --> 00:19:31,000 all your files. 356 00:19:31,700 --> 00:19:34,900 If I run this, see the output. It keeps on displaying, oh man, 357 00:19:35,700 --> 00:19:37,300 not 'list'. What happened? 358 00:19:37,300 --> 00:19:40,800 [no audio] 359 00:19:40,800 --> 00:19:43,400 argument must be str or bytes, not 'list'. 360 00:19:45,200 --> 00:19:46,700 'os.path.join()'. Yeah, 361 00:19:46,700 --> 00:19:48,500 sorry, this not a, 'each_file' 362 00:19:48,500 --> 00:19:49,500 you have to add it, right? 363 00:19:50,500 --> 00:19:51,300 Yeah, it's fine. 364 00:19:52,800 --> 00:19:57,100 See your operating system is able to display all your file 365 00:19:57,100 --> 00:19:59,200 systems from your entire operating system. 366 00:19:59,200 --> 00:19:59,300 Now. 367 00:19:59,300 --> 00:20:00,600 Now, it is working on 'C Drive'. 368 00:20:00,800 --> 00:20:03,400 I will wait some time so that you can see it will enter into 369 00:20:03,400 --> 00:20:04,000 some, yeah, now 370 00:20:04,000 --> 00:20:05,900 it is, still it is working with 'C drive' only. 371 00:20:06,900 --> 00:20:10,400 Yeah, you have to wait some time so that your operating system 372 00:20:10,400 --> 00:20:14,600 is going to get from each and every corner all the file names. 373 00:20:16,000 --> 00:20:18,700 You can run the same code on your Windows operating system, 374 00:20:18,700 --> 00:20:21,700 I mean your Windows operating system then so that your Python 375 00:20:21,700 --> 00:20:24,900 script is going to give all the file names with your complete 376 00:20:24,900 --> 00:20:28,000 path. Still it is working with the 'C Drive'. 377 00:20:28,000 --> 00:20:30,000 [no audio] 378 00:20:30,000 --> 00:20:31,000 Yes. 379 00:20:31,000 --> 00:20:39,900 [no audio] 380 00:20:39,900 --> 00:20:40,900 Just wait. 381 00:20:40,900 --> 00:20:45,800 [no audio] 382 00:20:45,800 --> 00:20:48,700 Each drive, drive names. Yes, fine. 383 00:20:48,800 --> 00:20:53,600 Let me go and enable this 'vd_names'. 384 00:20:53,600 --> 00:20:56,100 I will run from here then it will be, 385 00:20:56,100 --> 00:20:57,100 it would be clear. 386 00:20:57,100 --> 00:21:01,900 [no audio] 387 00:21:01,900 --> 00:21:04,300 'python search_a_file'. 388 00:21:05,300 --> 00:21:07,500 Yeah. Just give me some time guys. 389 00:21:07,500 --> 00:21:08,600 Just observe the output. 390 00:21:08,600 --> 00:21:11,300 [no audio] 391 00:21:11,300 --> 00:21:13,100 It is going to form automatically 392 00:21:14,200 --> 00:21:18,900 all paths for each and every file. Only for file we are writing 393 00:21:18,900 --> 00:21:22,700 a logic. Actually, our intention is just to search a file. The same way 394 00:21:22,700 --> 00:21:24,000 you can search for some folder 395 00:21:24,000 --> 00:21:25,800 also, I mean folder or directory. 396 00:21:27,000 --> 00:21:29,700 Right. Just wait and observe the output. 397 00:21:30,400 --> 00:21:32,900 It's working now with the 'C Drive'. 398 00:21:33,200 --> 00:21:34,900 After sometime it will enter into 'D Drive', 399 00:21:34,900 --> 00:21:37,400 I mean 'E Drive', and then 'F', because I don't have 'D Drive' 400 00:21:38,700 --> 00:21:41,000 Okay. Just observe the output. 401 00:21:41,000 --> 00:21:58,100 [no audio] 402 00:21:58,100 --> 00:22:02,800 Still your Python is working with your 'C Drive' only. You know 403 00:22:02,800 --> 00:22:05,500 in Windows under your operating system, right, 404 00:22:05,500 --> 00:22:08,000 you have a lot of, I mean the drive in which you have your 405 00:22:08,000 --> 00:22:10,000 operating system in that you have a lot of files, 406 00:22:10,100 --> 00:22:11,800 so it has to display all the files. 407 00:22:11,800 --> 00:22:12,800 Yes, it is displaying. 408 00:22:12,800 --> 00:22:15,400 [no audio] 409 00:22:15,400 --> 00:22:18,000 Right. Just wait some time. 410 00:22:18,100 --> 00:22:19,900 Finally, it will enter into other drivers as well. 411 00:22:20,100 --> 00:22:22,100 So once it has entered into other drive, then I will stop and 412 00:22:22,100 --> 00:22:23,600 I will write the remaining logic. 413 00:22:23,600 --> 00:22:27,700 [no audio] 414 00:22:27,700 --> 00:22:29,200 Still with 'C' only. 415 00:22:37,000 --> 00:22:38,400 You have that many number of files, 416 00:22:38,400 --> 00:22:39,500 that's why it's taking time. 417 00:22:39,500 --> 00:22:47,200 [no audio] 418 00:22:47,200 --> 00:22:49,000 Okay, still with 'C Drive'. 419 00:22:49,000 --> 00:22:54,900 [no audio] 420 00:22:54,900 --> 00:22:58,300 Because actually I installed so many softwares on my OS 421 00:22:59,500 --> 00:23:01,900 and anyway by default with your operating system also 422 00:23:01,900 --> 00:23:03,700 have so many files in your Windows, 423 00:23:04,200 --> 00:23:05,800 so it is going to display all of them. 424 00:23:05,800 --> 00:23:12,200 [no audio] 425 00:23:12,200 --> 00:23:15,500 So same logic you can also use to print only suppose some 426 00:23:15,800 --> 00:23:18,700 Python files, or some text file, something like that. 427 00:23:20,000 --> 00:23:26,800 Okay. Okay, at least we have to wait up to the next level directory. 428 00:23:26,800 --> 00:23:53,600 [no audio] 429 00:23:53,600 --> 00:23:57,800 Do one thing, just run your script in your, I mean on your 430 00:23:57,800 --> 00:23:58,500 operating system. 431 00:23:58,600 --> 00:24:00,500 If you get any challenges, just let me know. 432 00:24:01,100 --> 00:24:04,900 I will do it. So I don't want to waste your time by observing this output. 433 00:24:05,100 --> 00:24:06,600 Let me stop this code. 434 00:24:06,600 --> 00:24:09,600 [no audio] 435 00:24:09,600 --> 00:24:12,900 Right. Now what I have to do is, I don't want to print each and every 436 00:24:12,900 --> 00:24:18,000 file, right. If the file which is your required file, right, 437 00:24:18,000 --> 00:24:22,800 'if each_file', if it is '=req_file', 438 00:24:22,800 --> 00:24:24,600 [no audio] 439 00:24:24,600 --> 00:24:26,500 then only I want to print that. 440 00:24:26,500 --> 00:24:28,400 [no audio] 441 00:24:28,400 --> 00:24:29,200 That's it. Now, 442 00:24:29,200 --> 00:24:30,900 this is for your Windows operating system. 443 00:24:32,300 --> 00:24:33,300 Right. Fine. 444 00:24:33,400 --> 00:24:36,000 I will do one thing, from your 'C Drive' itself 445 00:24:36,000 --> 00:24:37,500 I will take some file name. Then 446 00:24:37,500 --> 00:24:38,300 that's good, right? 447 00:24:39,500 --> 00:24:40,500 What I will do is, 448 00:24:40,500 --> 00:24:44,700 [no audio] 449 00:24:44,700 --> 00:24:45,700 any file name, right, 450 00:24:45,700 --> 00:24:47,000 you can take any file name. Now 451 00:24:47,000 --> 00:24:48,500 I am going to run/ Before going to run 452 00:24:48,500 --> 00:24:51,100 let me enable which file you want to search. 453 00:24:51,100 --> 00:24:53,200 [no audio] 454 00:24:53,200 --> 00:24:54,900 Now I am running your Python script. 455 00:24:55,900 --> 00:24:57,600 It is asking, "Enter your required file: ". 456 00:24:57,600 --> 00:25:02,300 I am entering. Right. So it is going to look in your 'C', 'E', and 'F' drives. 457 00:25:02,500 --> 00:25:05,800 So it's searching. Definitely you will get after sometime your required files. 458 00:25:05,800 --> 00:25:08,900 Yes, the files which are ending with your file names you are getting them. 459 00:25:09,500 --> 00:25:11,700 You have same file name in different locations, 460 00:25:11,900 --> 00:25:14,500 you're going to get all of them. That's it. 461 00:25:15,100 --> 00:25:19,600 So guys, this is the way how you can search your file in your 462 00:25:19,600 --> 00:25:21,700 entire file system on your system. 463 00:25:22,900 --> 00:25:26,300 But if we observe, drives are there only with your Windows 464 00:25:26,300 --> 00:25:28,800 operating system, you don't have this type of drives on your 465 00:25:28,800 --> 00:25:29,700 Unix-like systems. 466 00:25:29,700 --> 00:25:33,700 Now your script should be platform-independent. How you 467 00:25:33,700 --> 00:25:37,000 can make your system, your script as platform-independent? 468 00:25:37,400 --> 00:25:43,300 See, if you observe, this is the code in our Unix-like systems, 469 00:25:43,300 --> 00:25:44,900 right. Very simple code. 470 00:25:46,400 --> 00:25:49,600 But in your Windows operating system you have this code. 471 00:25:49,800 --> 00:25:52,800 So this code won't work on your Unix-like systems, and 472 00:25:52,800 --> 00:25:55,900 that code, whatever the code you have on your Unix-like 473 00:25:55,900 --> 00:25:58,200 systems, that won't work on your Windows. 474 00:25:59,400 --> 00:26:01,500 Nothing is there. It's very simple. If you remember, 475 00:26:03,100 --> 00:26:08,800 we have a 'platform' module, right, and let me show you it on 476 00:26:08,800 --> 00:26:11,500 on your command line. If we 'import platform', 477 00:26:12,800 --> 00:26:15,100 and if you 'print(platform 478 00:26:16,300 --> 00:26:17,300 .system)', 479 00:26:17,900 --> 00:26:21,200 what is the output? Your operating system name. Now based 480 00:26:21,200 --> 00:26:22,400 on this what I am doing is, 481 00:26:24,100 --> 00:26:27,300 right, so this part is for your Windows, right. If, 482 00:26:28,300 --> 00:26:34,600 if your 'platform.system', if it is '=="Windows"', 483 00:26:34,600 --> 00:26:37,400 [no audio] 484 00:26:37,400 --> 00:26:38,500 then run the script, 485 00:26:39,800 --> 00:26:41,800 run this logic. 486 00:26:42,500 --> 00:26:43,500 So under 'if' condition 487 00:26:43,500 --> 00:26:44,300 you have to write this thing. 488 00:26:44,300 --> 00:26:46,100 That's why I am providing some indentations. 489 00:26:46,100 --> 00:26:51,100 [no audio] 490 00:26:51,100 --> 00:26:52,100 Right. 491 00:26:52,100 --> 00:26:57,000 [no audio] 492 00:26:57,000 --> 00:26:59,400 Let me modify your entire code, because for each and 493 00:26:59,400 --> 00:27:01,600 every line you have to provide now indentation so that your 494 00:27:01,600 --> 00:27:04,500 code would be there inside of your 'if' condition. 495 00:27:04,500 --> 00:27:08,700 [no audio] 496 00:27:08,700 --> 00:27:12,700 Then this is just to identify all your valid drives. Then second 497 00:27:12,700 --> 00:27:14,400 thing you are taking one by one drive, 498 00:27:14,600 --> 00:27:18,100 and then from your one by one drive, with the help of your 499 00:27:18,100 --> 00:27:19,100 'os.walk' 500 00:27:19,100 --> 00:27:22,500 you are creating all your files. Then after getting your one 501 00:27:22,500 --> 00:27:23,500 by one file, 502 00:27:24,000 --> 00:27:27,400 then you are checking whether that given file is matching 503 00:27:27,400 --> 00:27:31,100 with anyone of your found file with the help of 'os.walk'. 504 00:27:31,100 --> 00:27:35,700 If it is exactly a match then you are printing that. That's 505 00:27:35,700 --> 00:27:42,300 it. 'else' if it is not "Windows", then what you have to 506 00:27:42,300 --> 00:27:50,800 do? Just run your Unix-like system logic, this one. Anyway 507 00:27:50,800 --> 00:27:55,500 reading required search file is common. Now very simple code 508 00:27:55,500 --> 00:27:58,300 in your Unix. 509 00:27:58,300 --> 00:28:01,500 [no audio] 510 00:28:01,500 --> 00:28:02,800 Let me take one by one. 511 00:28:02,800 --> 00:28:07,200 [no audio] 512 00:28:07,200 --> 00:28:08,200 That's it. 513 00:28:08,200 --> 00:28:13,700 [no audio] 514 00:28:13,700 --> 00:28:14,700 One second. 515 00:28:14,700 --> 00:28:21,400 [no audio] 516 00:28:21,400 --> 00:28:25,400 So under this for loop you have a 'if' condition. 517 00:28:25,400 --> 00:28:28,000 So under that I want to write, that's why I am providing some space. 518 00:28:28,000 --> 00:28:29,800 [no audio] 519 00:28:29,800 --> 00:28:32,400 Right. Again under this you have a 'print' statement, 520 00:28:32,500 --> 00:28:34,100 then you should have some space. 521 00:28:34,100 --> 00:28:41,800 [no audio] 522 00:28:41,800 --> 00:28:42,800 That's it. 523 00:28:43,300 --> 00:28:45,200 Now, whatever the code you are having here, 524 00:28:45,400 --> 00:28:46,900 this will work on your Windows. 525 00:28:47,300 --> 00:28:50,300 The same code will work on your Unix-like systems as well. 526 00:28:50,300 --> 00:28:52,500 Let me show you first on your Unix-like systems. 527 00:28:53,200 --> 00:28:58,100 Now, I created a Python script, and it is like platform-independent 528 00:28:58,100 --> 00:29:02,000 script to search your required file in your entire operating 529 00:29:02,000 --> 00:29:06,100 system. Let me copy it. Right. 530 00:29:07,600 --> 00:29:08,600 Let me run it. 531 00:29:08,600 --> 00:29:12,800 [no audio] 532 00:29:12,800 --> 00:29:16,500 Let me provide 'host.conf' file 533 00:29:17,200 --> 00:29:20,800 Yes, you are getting. Let me run it and provide some 534 00:29:20,800 --> 00:29:22,600 [no audio] 535 00:29:22,600 --> 00:29:26,600 'sshd_config' file. 536 00:29:27,500 --> 00:29:28,800 Yes, you are. It's working, 537 00:29:28,800 --> 00:29:31,000 right. Same code now 538 00:29:31,000 --> 00:29:33,700 it is also going to work with your Windows as well. 539 00:29:33,800 --> 00:29:37,500 Now your script is finally platform-independent script. 540 00:29:38,600 --> 00:29:43,200 Right. So let me provide some file name which is there at very first. 541 00:29:43,200 --> 00:29:45,200 [no audio] 542 00:29:45,200 --> 00:29:47,200 Okay. Something, this is a text file. 543 00:29:48,100 --> 00:29:49,800 Let me copy this name. 544 00:29:49,800 --> 00:29:59,300 [no audio] 545 00:29:59,300 --> 00:30:04,000 '.txt'. Right. Now your Windows is going to search on your 546 00:30:04,100 --> 00:30:08,200 Windows operating system for your file, right. It is there 547 00:30:08,500 --> 00:30:12,600 I mean inside of some 'I' letter directory, so it will take 548 00:30:12,600 --> 00:30:14,600 some time to search for that. 549 00:30:14,900 --> 00:30:20,800 You just wait. So finally it will find that out. Otherwise, 550 00:30:20,800 --> 00:30:23,800 I will do one thing, very first based on alphabets, which 551 00:30:23,800 --> 00:30:25,400 is going to 'r' recursively, right? 552 00:30:25,400 --> 00:30:26,900 Yeah, fine. You have to take some time. 553 00:30:26,900 --> 00:30:29,400 [no audio] 554 00:30:29,400 --> 00:30:30,200 Hey, what happened? 555 00:30:30,300 --> 00:30:31,300 It stopped here. 556 00:30:31,300 --> 00:30:34,500 [no audio] 557 00:30:34,500 --> 00:30:36,700 Let me take exactly the file name. 558 00:30:36,700 --> 00:30:37,900 I will take simply, 559 00:30:39,400 --> 00:30:40,400 'python.exe'. 560 00:30:40,400 --> 00:30:42,600 [no audio] 561 00:30:42,600 --> 00:30:45,200 Maybe 'python.exe'. 562 00:30:46,800 --> 00:30:48,300 Yes, it's finding. 563 00:30:48,300 --> 00:30:52,000 Maybe I have given some extension maybe not exactly 564 00:30:52,000 --> 00:30:53,200 '.txt' for that. 565 00:30:53,600 --> 00:30:56,500 So after getting your files from 'C Drive', it is also looking 566 00:30:56,500 --> 00:30:58,200 for your other drives, that's why it is waiting. 567 00:30:59,200 --> 00:31:01,100 It will take some time because it is Windows, right? 568 00:31:01,100 --> 00:31:02,000 It will take some time. 569 00:31:02,500 --> 00:31:07,300 But finally we implemented a script simply to identify, to 570 00:31:07,300 --> 00:31:10,600 search a file in your entire file system of your operating 571 00:31:10,600 --> 00:31:12,500 system, I mean on your system, 572 00:31:13,700 --> 00:31:16,200 and that script is now platform-independent. 573 00:31:16,500 --> 00:31:19,900 Nothing is there. With the help of your 'if' and 'else' condition we have 574 00:31:19,900 --> 00:31:21,900 [no audio] 575 00:31:21,900 --> 00:31:25,500 written a script. This part will execute on your Windows, 576 00:31:26,000 --> 00:31:28,600 and if it is not Windows this script will execute. That's it. 577 00:31:30,000 --> 00:31:34,400 Right. So guys this type of logics are very, very important in your real time. 578 00:31:34,400 --> 00:31:37,400 And you can say this is best example from your 'os.walk' 579 00:31:37,400 --> 00:31:39,800 for your real-time situations. 580 00:31:40,800 --> 00:31:43,000 Okay. Okay guys, 581 00:31:43,000 --> 00:31:45,400 thank you for watching this video. Bye. 582 00:31:45,400 --> 00:31:46,200 [no audio]