1 00:00:00,000 --> 00:00:02,200 [no audio] 2 00:00:02,200 --> 00:00:03,700 Friends, here we are going to 3 00:00:03,700 --> 00:00:06,100 write one simple Python Script. That is, 4 00:00:07,100 --> 00:00:13,200 read a path and check if given path is a file or a directory. 5 00:00:13,600 --> 00:00:16,900 So you have to write one Python Script such that it has to 6 00:00:16,900 --> 00:00:19,600 read some path. After providing path, 7 00:00:20,100 --> 00:00:24,700 right, your python has to validate that, whether the given path is 8 00:00:24,700 --> 00:00:28,810 for file or is for directory, right. Fine. 9 00:00:28,800 --> 00:00:31,000 Let me open my editor first. 10 00:00:31,900 --> 00:00:36,700 So guys, we know if you want to read a path, path is always 11 00:00:36,700 --> 00:00:39,900 a string you need to remember that. Path is always a string. 12 00:00:39,900 --> 00:00:43,200 If you go with any operating system path is always a string. 13 00:00:43,900 --> 00:00:48,100 First let me save program as 'identify_given_path', 14 00:00:48,100 --> 00:00:49,400 or 'working_on_path'. 15 00:00:49,400 --> 00:00:52,210 [no audio] 16 00:00:52,200 --> 00:00:57,100 Let me take something like 'path_file_directory.py'. 17 00:00:58,200 --> 00:01:03,300 Fine. See my intention is, I want to read a path first. So path is 18 00:01:03,300 --> 00:01:04,000 always a string. 19 00:01:04,000 --> 00:01:06,110 That's why you know in your Python 20 00:01:06,099 --> 00:01:12,400 you have a 'input' syntax to read any string. Now I am taking 21 00:01:12,400 --> 00:01:16,200 variable as 'path', and I'm asking the user "Enter your path: ". 22 00:01:16,200 --> 00:01:19,710 [no audio] 23 00:01:19,700 --> 00:01:22,100 Right. So after that what I have to do? 24 00:01:23,100 --> 00:01:25,700 Whatever the path you are providing that path 25 00:01:25,700 --> 00:01:30,200 I have to check whether it is a file or directory path. Then 26 00:01:30,200 --> 00:01:34,310 how can I validate that? See, previously we discussed somewhere 27 00:01:34,300 --> 00:01:40,500 your 'os.path' sub-module of your 'os'. 'os.path.isfile()' 28 00:01:40,500 --> 00:01:43,300 'isdirectory'. You can go with anyone of that. 29 00:01:43,700 --> 00:01:46,410 So now I want to use 'os' module, 30 00:01:46,400 --> 00:01:48,400 that's why first you need to import your 'os', 31 00:01:48,400 --> 00:01:50,400 [no audio] 32 00:01:50,400 --> 00:01:53,000 then whatever the 'path' you are providing 33 00:01:53,000 --> 00:01:54,400 I'm checking with 'if' condition. 34 00:01:54,400 --> 00:01:57,600 'if os.path.isfile', 35 00:01:57,600 --> 00:02:01,500 that means if given path is a file then this will become 36 00:02:01,500 --> 00:02:04,800 'True'. If it is 'True', then whatever the logic you are going 37 00:02:04,800 --> 00:02:08,000 to write under your 'if' condition that will execute. Now what 38 00:02:08,000 --> 00:02:08,900 I want to display? 39 00:02:08,900 --> 00:02:11,810 [no audio] 40 00:02:11,800 --> 00:02:12,800 "The given 41 00:02:15,100 --> 00:02:17,200 path: ", whatever the path is there. 42 00:02:17,200 --> 00:02:19,609 [no audio] 43 00:02:19,600 --> 00:02:24,810 Sorry, "is a file". That's it. 44 00:02:24,800 --> 00:02:29,900 So in case if given path, given path is not a file, then this 45 00:02:29,900 --> 00:02:31,600 condition will become 'False'. 46 00:02:33,100 --> 00:02:34,609 That means that is not a file. 47 00:02:34,600 --> 00:02:37,510 If that is not a file, then what is our option? 48 00:02:37,500 --> 00:02:39,500 Definitely, it is a directory, right. Then 49 00:02:39,500 --> 00:02:41,000 let me do it, 'else' block 50 00:02:41,000 --> 00:02:42,100 I am writing, 'print(" 51 00:02:43,800 --> 00:02:49,700 The given path :", whatever the path you are providing that is, 52 00:02:50,700 --> 00:02:54,310 "is a directory path". That's it. 53 00:02:54,300 --> 00:02:55,300 Let me save it. 54 00:02:56,000 --> 00:02:57,600 Now I want to run this. 55 00:02:57,600 --> 00:02:59,000 So let me open my command line. 56 00:02:59,000 --> 00:03:00,800 [no audio] 57 00:03:00,800 --> 00:03:04,600 So already I am in the location where you have your script, this one. 58 00:03:04,600 --> 00:03:06,600 Let me run it with your 'python3'. 59 00:03:07,400 --> 00:03:09,800 I am working with Python 3 only guys. Sorry. 60 00:03:10,300 --> 00:03:13,300 'python --' 61 00:03:13,300 --> 00:03:15,600 [no audio] 62 00:03:15,600 --> 00:03:17,500 Let me do simply this thing. Yeah. 63 00:03:17,900 --> 00:03:22,704 Now let me run your script, that is, 'path_file_dir.py' 64 00:03:23,300 --> 00:03:24,700 Yes, it is asking something. 65 00:03:25,100 --> 00:03:29,300 So guys, we are working on Windows, right. See whenever you 66 00:03:29,300 --> 00:03:33,300 if you are working with Windows you always, you have to always 67 00:03:33,300 --> 00:03:37,100 provide your path as, I mean while entering your path you 68 00:03:37,100 --> 00:03:41,510 have to provide '\\' in your path instead of '\'. 69 00:03:41,500 --> 00:03:43,200 You know what is the reason. We already discussed that in 70 00:03:43,200 --> 00:03:44,609 our previous sessions. 71 00:03:44,600 --> 00:03:50,100 Now let me enter it and see the output. "The given path : ", "is a directory". 72 00:03:50,100 --> 00:03:51,910 Yes, you are getting the thing. 73 00:03:51,900 --> 00:03:55,200 Now, let me rerun and I am providing same thing, 74 00:03:55,200 --> 00:03:58,800 but at the end I am giving our Python Script name itself, no problem. 75 00:03:58,800 --> 00:03:59,800 You can provide anything. 76 00:03:59,800 --> 00:04:01,700 [no audio] 77 00:04:01,700 --> 00:04:06,600 Now see the result what you are getting, "is a file". Right. Now 78 00:04:07,900 --> 00:04:08,900 let me rerun, 79 00:04:10,300 --> 00:04:12,000 and I am giving something called, 80 00:04:13,000 --> 00:04:14,900 some "x: 81 00:04:14,900 --> 00:04:18,800 [no audio] 82 00:04:18,899 --> 00:04:22,500 \\python". So this path I am giving, but output you're getting, 83 00:04:22,500 --> 00:04:23,500 "is a directory". 84 00:04:24,000 --> 00:04:27,399 But if you observe, this path is not there in my host. If you 85 00:04:27,399 --> 00:04:32,800 go with my system, see that I have only C, E, and F, G directories. 86 00:04:32,800 --> 00:04:35,700 I don't have this directory no, 'X'. 'X' Drive 87 00:04:35,700 --> 00:04:38,400 I don't have but still you are getting information as "is a directory". 88 00:04:39,000 --> 00:04:41,900 It's absolutely wrong. Why you are getting this situation 89 00:04:41,900 --> 00:04:46,900 means, you are checking if given thing is a file, 90 00:04:46,900 --> 00:04:47,900 then you are executing this. 91 00:04:48,600 --> 00:04:53,100 So whether it is a directory or if it is invalid then at 92 00:04:53,100 --> 00:04:56,500 that time by default this result would become 'False', at that 93 00:04:56,500 --> 00:04:57,710 time you're executing 'else'. 94 00:04:57,700 --> 00:05:01,300 That's why what I have to do is before checking whether that 95 00:05:01,300 --> 00:05:02,700 is a file or directory, 96 00:05:02,700 --> 00:05:05,800 first of all you have to check whether that given path is 97 00:05:05,800 --> 00:05:06,800 existing or not. 98 00:05:07,900 --> 00:05:11,800 Right. See that's why what I am doing is, let me delete your entire code. 99 00:05:11,800 --> 00:05:18,300 [no audio] 100 00:05:18,300 --> 00:05:23,500 Now what I am doing is, 'if os.path.exist', 101 00:05:24,900 --> 00:05:28,300 if a given path is existing, then only I want to identify 102 00:05:28,300 --> 00:05:29,610 whether that is a file or not. 103 00:05:29,600 --> 00:05:31,300 If it is not existing I don't want to do it. 104 00:05:31,300 --> 00:05:33,300 [no audio] 105 00:05:33,300 --> 00:05:36,500 Right. Fine. Now I am saying simply. 106 00:05:37,700 --> 00:05:39,000 "Given path is valid". 107 00:05:39,000 --> 00:05:41,200 [no audio] 108 00:05:41,200 --> 00:05:44,000 If it is valid then only I can identify whether it is a 109 00:05:44,000 --> 00:05:47,700 file or not, 'else: print()'. 110 00:05:47,700 --> 00:05:51,510 [no audio] 111 00:05:51,500 --> 00:05:55,300 "Given path", so if you want to write that path you can write 112 00:05:55,300 --> 00:05:59,900 it, right, "is not existing on this host". 113 00:05:59,900 --> 00:06:02,310 [no audio] 114 00:06:02,300 --> 00:06:03,300 That's it. 115 00:06:03,300 --> 00:06:05,200 [no audio] 116 00:06:05,200 --> 00:06:07,600 "Given path", whatever the path you are giving let me write 117 00:06:07,600 --> 00:06:10,000 that path, "is a valid" one, 118 00:06:10,000 --> 00:06:13,300 first thing, is a valid path. First 119 00:06:13,300 --> 00:06:16,600 let me run this, then we will add your code. 120 00:06:16,600 --> 00:06:20,010 [no audio] 121 00:06:20,000 --> 00:06:22,900 Not. Let me go to command line. 122 00:06:22,900 --> 00:06:24,000 Let me run it. 123 00:06:24,000 --> 00:06:26,300 I'm running and I am providing, before running 124 00:06:26,300 --> 00:06:27,200 let me do this thing. 125 00:06:27,200 --> 00:06:29,710 [no audio] 126 00:06:29,700 --> 00:06:30,810 This is your code. 127 00:06:30,800 --> 00:06:36,300 Now, I'm running, and I'm going to provide some random thing, see 128 00:06:36,300 --> 00:06:38,100 "x: is not existing on this host". 129 00:06:38,100 --> 00:06:39,400 Actually, that is not a valid path 130 00:06:39,400 --> 00:06:42,610 that's why we are getting, that is not existing on this path. 131 00:06:42,600 --> 00:06:45,610 Now, let me rerun and provide some valid path. 132 00:06:45,600 --> 00:06:47,900 See this is a valid path, right. Whether it is a file or not 133 00:06:47,900 --> 00:06:51,100 secondary. First of all, I am checking whether that is existing 134 00:06:51,100 --> 00:06:52,100 on your host or not. 135 00:06:53,000 --> 00:06:54,000 That's what you're doing here. 136 00:06:54,000 --> 00:06:55,600 'os.path.exists'. 137 00:06:56,500 --> 00:07:02,300 See that. "Given path is valid path", even if you provide directory 138 00:07:02,300 --> 00:07:05,900 also, it will work. See up to this, this is a directory path, 139 00:07:05,900 --> 00:07:12,409 right. It's working. So after verifying that is a valid path, 140 00:07:12,400 --> 00:07:14,700 right, after that you have to check whether that is a directory 141 00:07:14,700 --> 00:07:18,100 or not. That's why inside of your 'if' condition you have to 142 00:07:18,100 --> 00:07:24,600 write one more 'if' condition, that is 'if os.path.isfile, 143 00:07:25,000 --> 00:07:27,900 then, then I can write 'print 144 00:07:29,500 --> 00:07:31,500 ("Given path : "', whatever the 145 00:07:31,500 --> 00:07:35,000 path is there simply I will write, and "it is a", 146 00:07:35,000 --> 00:07:37,600 [no audio] 147 00:07:37,600 --> 00:07:38,600 and "it is a 148 00:07:39,700 --> 00:07:41,000 file path", 149 00:07:42,700 --> 00:07:44,700 'else:'. 'else:' 150 00:07:45,000 --> 00:07:49,600 See first of all, after existence of your path you are entering 151 00:07:49,600 --> 00:07:53,200 into this block, inside of that your 'if' condition once again, 152 00:07:53,200 --> 00:07:55,600 we are verifying whether that is a file or not with your 153 00:07:55,600 --> 00:07:58,700 'os.path.isfile'. 'isfile', 154 00:07:58,700 --> 00:07:59,700 this will become 'true', 155 00:07:59,700 --> 00:08:02,100 then you are printing, "and it is a file path". 156 00:08:02,100 --> 00:08:07,400 If it is not, then what you are displaying, "and it is a directory 157 00:08:08,900 --> 00:08:09,900 path". That's it. 158 00:08:11,300 --> 00:08:13,500 Right. See that. I am going to run, 159 00:08:13,500 --> 00:08:15,460 [no audio] 160 00:08:15,450 --> 00:08:19,300 and I'm going to provide. See that. "valid path", and "it is a 161 00:08:19,300 --> 00:08:25,300 directory". Right. Then rerun and provide some file path. 162 00:08:25,400 --> 00:08:28,800 Yes. First of all, it "is a valid path", "and it is a file path". 163 00:08:28,800 --> 00:08:29,800 That's it. 164 00:08:30,700 --> 00:08:32,299 Right. So guys, this is the way 165 00:08:32,299 --> 00:08:35,799 simply how you can verify given path is 166 00:08:35,700 --> 00:08:38,200 a file path or directory path. 167 00:08:38,700 --> 00:08:43,700 So make sure that before validating your path is a file or directory path 168 00:08:43,799 --> 00:08:45,200 first of all you should check it, 169 00:08:45,200 --> 00:08:48,299 you should verify it whether that given path is existing 170 00:08:48,299 --> 00:08:49,299 on your host or not. 171 00:08:50,700 --> 00:08:53,900 If it is not existing, then you can't identify whether it 172 00:08:53,900 --> 00:08:55,000 is a file or directory. 173 00:08:55,000 --> 00:08:57,010 [no audio] 174 00:08:57,000 --> 00:08:58,100 Right. Fine. 175 00:08:58,800 --> 00:09:02,800 So this is the way how you can work with your Python 176 00:09:02,800 --> 00:09:05,500 to identify your paths, 177 00:09:05,500 --> 00:09:07,100 I mean like file or directory. 178 00:09:08,700 --> 00:09:10,000 Right. Okay. 179 00:09:10,000 --> 00:09:11,900 Okay guys, thank you for watching this video. 180 00:09:11,900 --> 00:09:16,400 [no audio]