1 00:00:00,000 --> 00:00:01,370 [no audio] 2 00:00:01,370 --> 00:00:05,730 Friends here we are going to discuss about 'os.path' module. 3 00:00:06,060 --> 00:00:11,190 Actually, this 'os.path' is a sub-module of 'os'. Let me open my 4 00:00:11,220 --> 00:00:17,300 command line. I'm going to open my Linux terminal. So here let 5 00:00:17,310 --> 00:00:22,920 me enter into my Python 3 terminal, right. So here I am going 6 00:00:22,930 --> 00:00:27,030 to 'import os'. Now you know, you can see the list of operations 7 00:00:27,040 --> 00:00:33,870 with your 'os'. Now if you see, somewhere you have 'os.path', right. 8 00:00:34,110 --> 00:00:41,510 Let me go and see that. 'os.path'. This one, right. This is the 9 00:00:41,520 --> 00:00:47,150 sub-module of your 'os'. So I want to see what are the operations 10 00:00:47,160 --> 00:00:52,260 are there with 'os.path'. So you can do it once again 11 00:00:52,270 --> 00:00:56,410 in this way, 'os' directory are operations on your 'os.path'. 12 00:00:56,420 --> 00:00:59,590 See that. You're getting some of the operations from your 13 00:00:59,600 --> 00:01:06,470 'os.path', right. And this 'os.path' sub-module is used 14 00:01:06,480 --> 00:01:10,780 to work on path, like you know whether particular path is there 15 00:01:10,790 --> 00:01:14,500 or not, whether that path is the directory or file path, right, 16 00:01:14,510 --> 00:01:18,190 what is the size of that, when it was created, 17 00:01:18,430 --> 00:01:19,690 what is the access time, 18 00:01:19,690 --> 00:01:23,240 what is the modified time, right, whether it is a soft link 19 00:01:23,250 --> 00:01:27,320 or not. So in this way to work with your path we are using 20 00:01:27,330 --> 00:01:33,230 'os.path' module, right. Let me go with some of them. First thing 21 00:01:33,240 --> 00:01:36,950 'os.path.sep'. We know 'os.sep' 22 00:01:36,960 --> 00:01:40,830 is also having your OS separator information. The same 23 00:01:40,840 --> 00:01:44,370 way 'os.path.sep' is also your operating system 24 00:01:44,400 --> 00:01:51,800 separator, right. Let me do one thing. Simply print 'os.path.sep'. 25 00:01:51,800 --> 00:01:54,300 You are getting your separator of your operating 26 00:01:54,300 --> 00:02:01,300 system, right. Then next one, 'os.path.basename(path)' 27 00:02:01,300 --> 00:02:03,700 First of all let me do one thing guys. 28 00:02:03,700 --> 00:02:08,300 [no audio] 29 00:02:08,300 --> 00:02:10,000 Suppose I'm going to take 30 00:02:10,000 --> 00:02:11,800 [no audio] 31 00:02:11,800 --> 00:02:15,500 a path called '/home/ec2-user', 32 00:02:15,570 --> 00:02:25,390 and then some, okay, let me simply copy this path, and I'm going 33 00:02:25,400 --> 00:02:29,170 to enter into my Python terminal, or you can also open some 34 00:02:29,180 --> 00:02:29,950 script, no problem. 35 00:02:29,950 --> 00:02:38,210 Let me do it, this thing otherwise 'vim os_path.py'. I am going 36 00:02:38,220 --> 00:02:43,290 to import first 'os' module, right, then I'm going to take a 37 00:02:43,300 --> 00:02:48,940 path in this way guys. I don't want to again and again type 38 00:02:48,900 --> 00:02:53,300 this path, that's why I'm storing my path into a variable called 'path'. 39 00:02:53,320 --> 00:03:00,109 Now what I want to do is, see guys 'basename' means, right, see 40 00:03:00,100 --> 00:03:02,800 in this entire path, let me show you that first. 41 00:03:02,800 --> 00:03:09,300 'print(os.path.basename(path))'. 42 00:03:09,300 --> 00:03:11,000 See the output what you are getting. 43 00:03:12,610 --> 00:03:17,450 Right. Let me save this and run this with your Python 3. 44 00:03:17,460 --> 00:03:23,590 See the output, right. See, in your given path, basename means 45 00:03:23,600 --> 00:03:28,440 this is, what is your exact last ending value in your path? 46 00:03:28,440 --> 00:03:33,490 It may be directory or path, right. You have one more thing, 47 00:03:33,500 --> 00:03:42,400 that is, let me print now 'os.path.dirname(path)', 48 00:03:42,400 --> 00:03:45,800 that means whatever the file or path, complete path you 49 00:03:45,870 --> 00:03:48,230 are providing, in that path 50 00:03:48,290 --> 00:03:49,640 what is the directory name? 51 00:03:49,940 --> 00:03:53,330 I mean, first let me run this. Before running this 52 00:03:53,340 --> 00:03:56,090 let me open your script once and then I'm going to run. 53 00:03:56,420 --> 00:04:00,890 So actually you are running a script to know what is the 54 00:04:00,900 --> 00:04:02,270 directory name of your path. 55 00:04:02,280 --> 00:04:04,010 See guys, if you take this is the path. 56 00:04:04,300 --> 00:04:07,540 This is called directory name for this file. 57 00:04:09,000 --> 00:04:11,870 Right. See the output. You're getting that one 58 00:04:11,880 --> 00:04:13,760 only. 'basename' means this part 59 00:04:13,770 --> 00:04:16,339 you are getting, directory name means you are getting this 60 00:04:16,300 --> 00:04:18,810 path. So this is very, very helpful whenever 61 00:04:18,800 --> 00:04:20,700 if you are installing some packages. 62 00:04:20,700 --> 00:04:23,000 [no audio] 63 00:04:23,000 --> 00:04:24,200 Right. Fine. 64 00:04:24,269 --> 00:04:28,810 Then what is the next one, 'os.path.join'. 65 00:04:28,820 --> 00:04:31,210 So this is used to join two paths. 66 00:04:31,200 --> 00:04:35,700 Suppose guys, just assume that you don't have this 'os.path.join' operation. 67 00:04:35,940 --> 00:04:38,310 Now what I want to do is, I want to add two paths. 68 00:04:38,310 --> 00:04:41,040 Guys, let me enter into my Python terminal. 69 00:04:41,050 --> 00:04:42,270 So before going to enter. 70 00:04:43,280 --> 00:04:44,730 Let me do 71 00:04:44,740 --> 00:04:49,110 this. Now I'm going to enter into Python. 72 00:04:49,120 --> 00:04:50,490 Let's say I have two paths. 73 00:04:51,040 --> 00:04:57,600 Path one is suppose '/home', then path two 74 00:04:57,700 --> 00:05:00,700 I'm having some 'ec2-user'. 75 00:05:00,700 --> 00:05:07,200 [no audio] 76 00:05:07,200 --> 00:05:09,000 Now I want to add these two paths. 77 00:05:09,100 --> 00:05:11,400 You know these two are strings, you can add it. 78 00:05:11,440 --> 00:05:15,000 Like if you remember you can add two strings in this way. 79 00:05:15,010 --> 00:05:17,760 String one plus string two. 80 00:05:19,300 --> 00:05:20,740 Right. See the result? 81 00:05:20,740 --> 00:05:25,450 Yes you are getting. But these two are the paths. Between these 82 00:05:25,400 --> 00:05:28,300 two paths you should have path separators as well. 83 00:05:28,310 --> 00:05:29,440 That means manually 84 00:05:29,470 --> 00:05:30,790 you need to add your path 85 00:05:30,850 --> 00:05:37,140 separator. 'path1 +', your path separator of your operating 86 00:05:37,150 --> 00:05:39,630 system, now I'm working with Unix operating system 87 00:05:39,640 --> 00:05:40,710 that's why I need to do, 88 00:05:41,200 --> 00:05:43,360 I need to provide this '/', if it is Windows 89 00:05:43,300 --> 00:05:46,600 other '\' you have to provide, right, then 'path2'. 90 00:05:47,600 --> 00:05:48,400 In this way you can get it. 91 00:05:48,400 --> 00:05:50,400 [no audio] 92 00:05:50,400 --> 00:05:52,700 But whenever if you are writing a script 93 00:05:54,000 --> 00:05:55,000 in that you are doing, 94 00:05:55,070 --> 00:05:56,810 you are adding some two paths. 95 00:05:57,100 --> 00:06:00,370 But based on operating system you need to provide slash. 96 00:06:01,500 --> 00:06:03,900 But if you take this one, I mean 97 00:06:04,900 --> 00:06:06,500 let me take import 'os'. 98 00:06:07,200 --> 00:06:10,500 Let me print now 'os.path.join', 99 00:06:11,000 --> 00:06:12,900 just I am providing 'path1' and 'path2'. 100 00:06:13,010 --> 00:06:16,070 I am not giving the separator to add these two paths. 101 00:06:16,070 --> 00:06:18,560 Python will take care based on your operating system. 102 00:06:19,200 --> 00:06:22,300 See that I'm not giving this '/' no. By default 103 00:06:22,420 --> 00:06:24,310 your Python is providing that '/'. 104 00:06:24,320 --> 00:06:27,380 If it is Windows, it will provide different slash. 105 00:06:27,500 --> 00:06:29,600 If it is Unix it is providing this one. 106 00:06:29,660 --> 00:06:33,990 So automatically your Python will add by providing your path 107 00:06:34,000 --> 00:06:35,340 separator between two paths. 108 00:06:35,700 --> 00:06:39,930 So it is very, very important guys, adding two paths. See, 109 00:06:39,940 --> 00:06:41,370 two paths are strings. 110 00:06:41,430 --> 00:06:43,620 You can also add your strings in this way. 111 00:06:43,630 --> 00:06:47,020 But whenever if you are doing in this way based on the operating 112 00:06:47,030 --> 00:06:48,580 system you have to provide your slash. 113 00:06:48,600 --> 00:06:51,700 But if you take 'os.path.join' Python will take care 114 00:06:51,700 --> 00:06:52,800 about your separator. 115 00:06:52,900 --> 00:06:55,100 That's it. Simple no. 116 00:06:56,200 --> 00:06:58,900 Then 'os.path.split'. 117 00:06:58,980 --> 00:07:02,580 See whatever the path you are having in your hand, right, 118 00:07:02,640 --> 00:07:07,290 if you split that, it is going to split the path name into 119 00:07:07,300 --> 00:07:09,750 a pair which consists of head and tail. 120 00:07:09,980 --> 00:07:13,730 I mean, let's say somewhere we have path, right? 121 00:07:13,740 --> 00:07:18,050 Let me take this path. Assume that this is a path. This is 122 00:07:18,060 --> 00:07:22,990 a path. So in this path this is called head, and this is called 123 00:07:23,000 --> 00:07:28,010 tail. See if I do simply 'os', let me print the result directly, 124 00:07:28,550 --> 00:07:31,340 and guys you can also assign this operation's output into 125 00:07:31,350 --> 00:07:35,930 some variable. 'os.path.split()', your path, 126 00:07:35,940 --> 00:07:37,790 whatever the path you are having in your hand. 127 00:07:37,800 --> 00:07:40,910 See the result. You are getting a tuple which consists of 128 00:07:41,540 --> 00:07:46,780 head and tail. Now this operation output you can assign you know, this 129 00:07:46,790 --> 00:07:52,760 is you know 'path.dirname' output. This is 'basename' command output. 130 00:07:52,790 --> 00:07:57,890 You have done this, right. 'basename' and then 'dirname', that's 131 00:07:57,900 --> 00:08:02,170 what it is giving. 'split' operation is going to give 'dirname', 132 00:08:02,230 --> 00:08:09,680 and then 'basename' outputs. Fine. Now, just get the path size, 133 00:08:09,830 --> 00:08:12,020 I mean whatever the file you have in your hand, 134 00:08:12,110 --> 00:08:17,230 I want to get the size for that. See 'os.path.getsize()' is 135 00:08:17,240 --> 00:08:20,560 the operation. See, nothing is there. You don't worry. Where 136 00:08:20,500 --> 00:08:24,100 these operations are there? 'dir(os.path)' 137 00:08:24,100 --> 00:08:26,200 It will give a list of operations. From that I'm taking some 138 00:08:26,200 --> 00:08:27,600 of them for time being. 139 00:08:29,200 --> 00:08:31,000 And by default this operation will 140 00:08:31,000 --> 00:08:32,900 give your result in terms of bytes. 141 00:08:34,000 --> 00:08:37,400 Right. See what I'm saying is, don't need to remember all these operations 142 00:08:37,400 --> 00:08:41,700 just have a glance once here. So from this I'm taking some of them. 143 00:08:41,700 --> 00:08:44,000 [no audio] 144 00:08:44,000 --> 00:08:48,200 Right. Sorry, not 'os.sep', right, 'os.path'. 145 00:08:48,200 --> 00:08:51,000 [no audio] 146 00:08:51,000 --> 00:08:52,000 That's it. 147 00:08:52,900 --> 00:08:57,900 Fine. Now, next one is, 'os.path.exists()'. 148 00:08:57,900 --> 00:09:00,900 See guys, just assume that you are going to start some application, 149 00:09:01,200 --> 00:09:02,500 to start some application 150 00:09:02,500 --> 00:09:05,500 you have some command for that application on your operating 151 00:09:05,560 --> 00:09:09,490 system. So to start that application I need to run some particular 152 00:09:09,500 --> 00:09:12,010 file, but before going to run make sure that whether it is 153 00:09:12,020 --> 00:09:15,200 there or not. If it is not there you can't do anything, right. 154 00:09:15,210 --> 00:09:19,450 That's why just for 'if' condition purpose, right, you can use 155 00:09:19,460 --> 00:09:25,840 this one, 'os.path.exists()', your path. If really your 156 00:09:25,850 --> 00:09:29,360 path is existing, guys our path is, I stored somewhere 'path' 157 00:09:29,300 --> 00:09:33,800 as this one. If 'test.py' is there, then only I can run it. Yes, 158 00:09:33,800 --> 00:09:37,800 you are getting 'True'. See what I am saying is, I can write 159 00:09:37,830 --> 00:09:41,850 a simple Python script. In this I am taking in this way. 160 00:09:42,700 --> 00:09:50,600 'if os.path.exists(path)', your given path, then I can say, 'print( 161 00:09:51,600 --> 00:09:52,800 "your file is there")'. 162 00:09:52,800 --> 00:09:55,800 [no audio] 163 00:09:55,800 --> 00:10:00,800 Right. See let me run it. 'else', else if that is 'False' then I can say 164 00:10:00,800 --> 00:10:05,000 that print this, let me print it. 165 00:10:05,000 --> 00:10:07,100 [no audio] 166 00:10:07,100 --> 00:10:08,600 "is not present 167 00:10:08,600 --> 00:10:11,100 [no audio] 168 00:10:11,100 --> 00:10:13,600 on this host". That's it. 169 00:10:14,500 --> 00:10:16,800 Right. Let me run it once. 170 00:10:16,800 --> 00:10:19,700 [no audio] 171 00:10:19,700 --> 00:10:23,000 'python3', script. Yes, you are getting, "your file 172 00:10:23,100 --> 00:10:27,290 is there". What I'm doing is, I'm just going to modify my path 173 00:10:27,500 --> 00:10:30,620 value, something like that, 'ec2-user' is there 174 00:10:30,630 --> 00:10:32,060 no, I'm removing some 'e' letter. 175 00:10:32,140 --> 00:10:34,390 Now, that is not a valid path on my host. 176 00:10:34,390 --> 00:10:38,840 Now if I run that, see, "is not present on this host". Yes, it is 177 00:10:38,800 --> 00:10:43,000 not present. So to take some decision based on your file 178 00:10:43,000 --> 00:10:46,100 with 'if' condition this is also helpful. 179 00:10:46,740 --> 00:10:52,000 Right. Then, see given path maybe file path or directory path. 180 00:10:52,280 --> 00:10:57,290 You can check it. 'os.path.isfile()'. In case whatever the path 181 00:10:57,290 --> 00:10:58,160 you are providing, 182 00:10:58,200 --> 00:11:02,200 if that is the file path then this value will give output as 'True'. 183 00:11:02,550 --> 00:11:07,630 Then if it is a directory path then 'os.path.isdir()' 184 00:11:07,690 --> 00:11:09,550 will give that output as a directory. 185 00:11:09,600 --> 00:11:13,080 Now see that, what I am doing is, I want to decide. 186 00:11:13,090 --> 00:11:16,530 I want to decide whether the given path is a file or not. 187 00:11:17,000 --> 00:11:18,740 Then what I will do? 188 00:11:19,300 --> 00:11:21,000 Let me comment all these lines. 189 00:11:22,600 --> 00:11:24,400 Right. I'm going to comment all these lines. 190 00:11:25,500 --> 00:11:28,800 Now, I'm implementing a simple script that is, 191 00:11:29,400 --> 00:11:32,800 'if os.path.isfile()', 192 00:11:33,080 --> 00:11:35,690 whatever the path you are having in your hand, then I can 193 00:11:35,700 --> 00:11:36,700 say that, 'print( 194 00:11:36,700 --> 00:11:40,900 [no audio] 195 00:11:40,900 --> 00:11:42,400 "is a file")'. 196 00:11:43,500 --> 00:11:44,900 That's it. 'else', 197 00:11:44,900 --> 00:11:47,400 if it is not a file we have only one other option, that is 198 00:11:48,400 --> 00:11:49,400 'print(" 199 00:11:49,400 --> 00:11:53,100 [no audio] 200 00:11:53,100 --> 00:11:54,900 'print("your path is a directory")'. 201 00:11:54,900 --> 00:11:58,400 [no audio] 202 00:11:58,400 --> 00:11:59,400 That's it. 203 00:12:00,800 --> 00:12:02,800 So I can use only 'isfile', 204 00:12:02,800 --> 00:12:03,800 and I can decide. 205 00:12:04,800 --> 00:12:10,000 Because if it is a file, then 'os.path.isfile' will become true. 206 00:12:10,300 --> 00:12:11,800 Then you are going to display this line. 207 00:12:13,400 --> 00:12:15,600 Right. See the output. Yes. 208 00:12:16,200 --> 00:12:20,200 Now what I am doing is, I'm going to modify your path. 209 00:12:20,200 --> 00:12:21,900 I'm going to give up to directory. 210 00:12:21,900 --> 00:12:24,600 [no audio] 211 00:12:24,600 --> 00:12:27,400 Right. Now, see the result what you're getting. 212 00:12:28,400 --> 00:12:29,700 "is a directory", you are getting. 213 00:12:29,800 --> 00:12:31,740 Yes, it's a directory. Previously you got file, 214 00:12:31,750 --> 00:12:35,500 now it is a directory. See now, to check that I can use either 215 00:12:35,510 --> 00:12:39,970 'isfile' operation or 'isdir' operation in your 'if' conditions. 216 00:12:41,200 --> 00:12:44,100 Then, 'os.path.islink()'. 217 00:12:44,200 --> 00:12:47,700 See, you know sometimes we have some soft links, right. 218 00:12:47,780 --> 00:12:48,760 Let me check it, 219 00:12:48,700 --> 00:12:49,800 is there any soft link? 220 00:12:49,800 --> 00:12:52,000 [no audio] 221 00:12:52,000 --> 00:12:54,000 No. Let me do one thing. 222 00:12:54,100 --> 00:12:55,900 I'm going to create a soft link, 223 00:12:57,400 --> 00:12:58,400 'os.path' 224 00:12:58,450 --> 00:13:01,380 I'm going to create 'myos', something like that. 225 00:13:01,580 --> 00:13:05,800 Now see that. Now 'myos' is a soft link, right? 226 00:13:05,800 --> 00:13:09,100 I want to check that with Python, whether it's a soft link or not. 227 00:13:09,800 --> 00:13:11,200 Simply import your 'os'. 228 00:13:11,500 --> 00:13:16,200 You can also write your script. I'm doing from command line. 229 00:13:16,400 --> 00:13:19,000 Right. See. 'print 230 00:13:20,200 --> 00:13:24,000 (os.path.islink)', 231 00:13:25,500 --> 00:13:27,100 then just provide your 232 00:13:27,160 --> 00:13:30,580 suppose, 'myos' file, or you can also provide complete path. 233 00:13:30,590 --> 00:13:31,990 As of now, I am in the same location 234 00:13:32,000 --> 00:13:34,600 that's why I'm not providing complete path. Yes, true. 235 00:13:35,460 --> 00:13:39,150 But what I am doing is, 'os.path.islink', 236 00:13:39,300 --> 00:13:40,700 I am providing suppose 237 00:13:42,000 --> 00:13:45,700 'os_path.py'. This is not a 238 00:13:45,780 --> 00:13:47,360 softlink. This is simply a file, right? 239 00:13:47,370 --> 00:13:49,070 That's why we're getting output as 'False'. 240 00:13:49,400 --> 00:13:53,720 So based on this True and False I can tell that this is a 241 00:13:53,700 --> 00:13:58,200 soft link and this is not a soft link by using 'if' condition. 242 00:13:59,800 --> 00:14:03,800 Right. So, guys, these all are some of the 243 00:14:03,840 --> 00:14:07,370 operations from your 'os.path', and which are very, very helpful 244 00:14:07,380 --> 00:14:12,170 in your real time. Along with that you also need access time, 245 00:14:12,900 --> 00:14:15,600 right, some creation time, modified time. 246 00:14:16,600 --> 00:14:17,900 So these times you 247 00:14:17,910 --> 00:14:22,160 can get from your 'os.path'. 'os.path.getatime'. 248 00:14:22,380 --> 00:14:25,560 But to understand these three operations you should be good 249 00:14:25,570 --> 00:14:28,890 with some time module of your Python. So after discussing 250 00:14:28,900 --> 00:14:32,600 time module once again we'll come back to your 'os.path', 251 00:14:32,600 --> 00:14:35,600 and I will explain about these three operations. 252 00:14:36,600 --> 00:14:38,400 Right. Okay. 253 00:14:38,400 --> 00:14:40,300 Okay guys, thank you for watching this video. 254 00:14:40,300 --> 00:14:48,300 [no audio]