1 00:00:00,000 --> 00:00:01,100 [no audio] 2 00:00:01,100 --> 00:00:02,400 Friends here we are going to 3 00:00:02,400 --> 00:00:04,900 implement one simple Python Script that is, 4 00:00:04,900 --> 00:00:09,000 simply how to change current working directory. See changing 5 00:00:09,000 --> 00:00:11,400 current working directory with Python is very easy. 6 00:00:11,600 --> 00:00:15,700 Let me enter into my Python 3 terminal, right, and I am going 7 00:00:15,700 --> 00:00:17,000 to import 'os' module. 8 00:00:17,500 --> 00:00:21,400 Let me show you that, 'os.', current working directory 9 00:00:21,400 --> 00:00:22,300 where we are. 10 00:00:22,600 --> 00:00:27,000 We know we have 'os.getcwd' syntax. Already 11 00:00:27,000 --> 00:00:30,700 we have seen all these syntaxes in our previous videos, right? 12 00:00:31,200 --> 00:00:32,799 Fine. Now suppose 13 00:00:32,799 --> 00:00:36,700 I want to move or change my current working directory to 14 00:00:36,900 --> 00:00:38,800 something, some location 15 00:00:38,800 --> 00:00:39,800 I am giving, some path 16 00:00:39,800 --> 00:00:42,600 I am giving. Now it has been executed. Now 17 00:00:42,600 --> 00:00:46,400 let me check it where we are now, 'getcwd', 18 00:00:46,500 --> 00:00:47,500 under 'root' it is see. 19 00:00:48,100 --> 00:00:52,000 So if you write these three lines, right, then your script is over. 20 00:00:52,700 --> 00:00:55,300 But sometimes while changing your directory you will get 21 00:00:55,300 --> 00:00:56,400 some exceptions. 22 00:00:57,000 --> 00:00:59,100 Let me show you them. Suppose 23 00:00:59,100 --> 00:01:04,599 what I am doing is, 'chdir("/etc/")'. 24 00:01:05,000 --> 00:01:06,300 I will do one thing first. 25 00:01:06,300 --> 00:01:08,400 [no audio] 26 00:01:08,400 --> 00:01:13,600 So let me move into 'tmp', and let me create 27 00:01:13,800 --> 00:01:18,300 'hi.txt', and let me create directory. 28 00:01:18,300 --> 00:01:22,000 [no audio] 29 00:01:22,000 --> 00:01:26,600 Fine. Now, I am entering into my 'python3'. 30 00:01:27,500 --> 00:01:30,300 Right. Let's make sure this is Python 3 syntaxes. 31 00:01:30,700 --> 00:01:32,900 So you have small differences with your Python 3, 32 00:01:33,200 --> 00:01:36,400 I mean compared to Python 2, right. Fine. First 33 00:01:36,400 --> 00:01:42,000 let me import 'os' module. Then 'os.getcwd', right? 34 00:01:42,200 --> 00:01:44,100 Then I am changing into suppose 35 00:01:45,500 --> 00:01:46,500 "/tmp". 36 00:01:46,500 --> 00:01:48,300 [no audio] 37 00:01:48,300 --> 00:01:49,900 Now, what is your current working directory? 38 00:01:51,500 --> 00:01:52,800 Fine. Now 39 00:01:53,000 --> 00:01:55,000 anyway we are in '/tmp', 40 00:01:55,100 --> 00:01:57,900 but just assume that you are in any location but I am trying 41 00:01:57,900 --> 00:01:59,500 to provide '/tmp', 42 00:02:00,200 --> 00:02:02,000 and then 'hi.txt'. 43 00:02:02,400 --> 00:02:04,200 So 'hi.txt' is a file, right? 44 00:02:04,200 --> 00:02:07,200 We can't move into that. In case you provide by mistake that 45 00:02:07,200 --> 00:02:10,800 as the path to change, right, through your Python, 46 00:02:11,100 --> 00:02:12,600 then you are going to get an exception. 47 00:02:13,000 --> 00:02:14,000 What is that exception? 48 00:02:14,900 --> 00:02:17,900 'NotADirectory' error, right. 49 00:02:17,900 --> 00:02:19,700 Just make a note of this. 50 00:02:20,400 --> 00:02:22,400 So that finally we'll create complete script. 51 00:02:22,700 --> 00:02:25,000 So this is one of the exception you are going to get. When 52 00:02:25,000 --> 00:02:26,000 you are going to get that, 53 00:02:27,600 --> 00:02:29,400 'NotADirectory' exception. 54 00:02:29,800 --> 00:02:36,700 Now, what I am doing is, 'os.chdir("/tmp/hello.txt"). 55 00:02:36,700 --> 00:02:38,800 [no audio] 56 00:02:38,800 --> 00:02:42,700 See that. Now what you are getting, 'FileNotFound' error because 57 00:02:42,700 --> 00:02:44,900 actually whatever the path you are providing, that is not 58 00:02:44,900 --> 00:02:47,500 present on your host. In that case you are going to get 59 00:02:47,500 --> 00:02:48,800 exception in this way. 60 00:02:50,400 --> 00:02:52,800 Right. Now, let me make a note on this as well. 61 00:02:52,800 --> 00:02:55,700 [no audio] 62 00:02:55,700 --> 00:02:59,700 Then let me move into one more directory. 63 00:03:00,300 --> 00:03:05,300 So suppose I have '/etc/httpd', there 64 00:03:05,300 --> 00:03:07,200 I have something file called 'conf'. 65 00:03:08,900 --> 00:03:10,900 Now see that. What is the exception you are getting. 66 00:03:11,600 --> 00:03:15,100 'PermissionError', so 'Permission denied'. Actually have a path but you 67 00:03:15,100 --> 00:03:18,700 don't have permission to enter into that directory, right? 68 00:03:19,000 --> 00:03:23,600 So changing directory, working directory is very simple, 69 00:03:24,000 --> 00:03:25,700 simply 'os.chdir'. 70 00:03:26,000 --> 00:03:29,200 But while working in your real time you will get these type 71 00:03:29,200 --> 00:03:33,100 of exceptions. Then whenever if you are writing a Python Script, 72 00:03:33,400 --> 00:03:35,100 it should work in all scenarios. 73 00:03:35,200 --> 00:03:38,100 That's why you need to think about all possibility exceptions. 74 00:03:38,700 --> 00:03:41,100 These are the possible exceptions you have 75 00:03:41,100 --> 00:03:43,000 for your 'chdir'. 76 00:03:43,500 --> 00:03:45,900 Now, you need to write a Python Script, right? 77 00:03:45,900 --> 00:03:50,100 So what I want to do is first I need to 'import os'. First 78 00:03:50,100 --> 00:03:53,500 I am going to implement this script in terms of, I mean without 79 00:03:53,500 --> 00:03:56,600 functions. Then after that I will show you in terms of simple 80 00:03:56,600 --> 00:04:00,600 function, so that you will get practice with your functions as well. 81 00:04:01,400 --> 00:04:06,700 Okay. Fine. 'import os'. So I am going to read a required path 82 00:04:06,900 --> 00:04:10,700 from the user. So 'req_path =', just use 'input' syntax. 83 00:04:11,000 --> 00:04:12,200 So, "Enter your 84 00:04:14,600 --> 00:04:19,800 change directory", "Enter path to change directory". 85 00:04:19,800 --> 00:04:24,200 [no audio] 86 00:04:24,200 --> 00:04:26,800 That's it. Right. Just for user understanding purpose 87 00:04:26,800 --> 00:04:28,300 we are giving this 'print' message. 88 00:04:28,800 --> 00:04:32,400 So before going to change, let me print simply, 89 00:04:32,400 --> 00:04:35,000 [no audio] 90 00:04:35,000 --> 00:04:39,600 "The current working directory is", so you know, how we can 91 00:04:39,600 --> 00:04:42,200 get with 'os', 'os.getcwd'. 92 00:04:43,900 --> 00:04:47,900 Then, now guys see you're going to get an exception, right, 93 00:04:48,300 --> 00:04:50,900 so you can easily handle with the 'try' and 'except' blocks. 94 00:04:51,000 --> 00:04:53,000 So what I am doing is, 'try:' 95 00:04:53,000 --> 00:04:55,100 [no audio] 96 00:04:55,100 --> 00:05:00,000 'try:', so what I am trying is 'os.chdir 97 00:05:01,100 --> 00:05:04,600 (req_path)'. So you are going to read the path from the 98 00:05:04,600 --> 00:05:11,300 input side, right. Now in case if it is works perfectly, then 99 00:05:11,300 --> 00:05:21,200 I can 'print', simply, "Now your new working directory is", then 100 00:05:21,200 --> 00:05:24,200 I can say simply 'os.getcwd()'. 101 00:05:25,400 --> 00:05:29,400 But while changing your path, while changing your directory you're 102 00:05:29,400 --> 00:05:30,900 going to get some exceptions, right. 103 00:05:30,900 --> 00:05:36,200 We have already taken those exceptions. Now in case very first 104 00:05:36,200 --> 00:05:37,500 chance let me take this one. 105 00:05:37,700 --> 00:05:41,100 The given path is not existing then, then you are going to 106 00:05:41,100 --> 00:05:42,100 get this exception, 107 00:05:42,100 --> 00:05:45,800 then I am handling this, 'except', in case 'FileNotFoundError', 108 00:05:46,100 --> 00:05:47,700 then I can say simply, 109 00:05:49,100 --> 00:05:50,100 "Please verify 110 00:05:50,100 --> 00:05:53,100 [no audio] 111 00:05:53,100 --> 00:05:54,100 given path", 112 00:05:55,100 --> 00:05:58,100 or "Given path is not a valid path". 113 00:05:58,100 --> 00:06:00,400 [no audio] 114 00:06:00,400 --> 00:06:06,000 "Given path is not a valid path". That's it. 115 00:06:07,100 --> 00:06:10,000 or sometimes you're providing valid path 116 00:06:10,000 --> 00:06:13,500 but you are going to provide file path, then 'except 117 00:06:14,500 --> 00:06:17,900 NotADirectory' exception. Then I can say 'print(" 118 00:06:17,900 --> 00:06:20,600 [no audio] 119 00:06:20,600 --> 00:06:25,600 Given path is a file path. 120 00:06:26,100 --> 00:06:28,800 So can't change 121 00:06:28,800 --> 00:06:31,100 [no audio] 122 00:06:31,100 --> 00:06:35,800 working directory")'. So same information if you want to write 123 00:06:35,800 --> 00:06:37,100 you can also write in your 124 00:06:38,900 --> 00:06:42,600 'FileNotFoundError' also. That's it. 125 00:06:42,600 --> 00:06:44,400 [no audio] 126 00:06:44,400 --> 00:06:47,200 Right. Then you have one more exception. Even though if you 127 00:06:47,200 --> 00:06:51,500 provide valid path valid directory path, sometimes you 128 00:06:51,500 --> 00:06:54,100 may not have access to enter into that directory, then 129 00:06:54,100 --> 00:06:56,500 'PermissionError', then print simply 130 00:06:58,200 --> 00:06:59,200 "Sorry, 131 00:06:59,200 --> 00:07:01,100 [no audio] 132 00:07:01,100 --> 00:07:02,100 you don't have 133 00:07:02,100 --> 00:07:04,600 [no audio] 134 00:07:04,600 --> 00:07:07,600 access to change". 135 00:07:10,600 --> 00:07:12,300 Okay, "You don't have access to", 136 00:07:13,600 --> 00:07:15,400 "access for the given 137 00:07:16,400 --> 00:07:22,900 path. So can't change", you can write any message, whatever you want. 138 00:07:24,600 --> 00:07:28,400 That's it. Other than this, in case if you get any other, 139 00:07:28,400 --> 00:07:32,000 any new exception, just to handle that exception, 'except 140 00:07:34,700 --> 00:07:38,000 Exception as e', let me print that exception. In case if you 141 00:07:38,000 --> 00:07:40,400 get any new exceptions, then you can handle them, 142 00:07:40,400 --> 00:07:41,500 you can see them here, 143 00:07:42,100 --> 00:07:44,800 so that in future you can add them, right? That's it. 144 00:07:46,500 --> 00:07:49,700 Right. Now let me exit from here, and I am going to write a 145 00:07:49,700 --> 00:07:53,200 simple Python Script, 'change_directory.py'. 146 00:07:53,200 --> 00:07:55,700 [no audio] 147 00:07:55,700 --> 00:07:56,700 That's it. 148 00:07:56,800 --> 00:08:02,700 Now, let me save it and let me run this with 'python3', so I didn't 149 00:08:02,700 --> 00:08:05,100 provide shebang line. 150 00:08:05,100 --> 00:08:07,500 If you want you can provide and directly you can write, by giving 151 00:08:07,500 --> 00:08:08,500 execution permission 152 00:08:08,500 --> 00:08:09,900 you can run './'. 153 00:08:11,100 --> 00:08:12,700 Okay, something error. 154 00:08:12,700 --> 00:08:13,700 I have given, 155 00:08:15,300 --> 00:08:19,200 where? Yeah, two quotations. Let me remove one quotation. 156 00:08:19,200 --> 00:08:20,200 Now, it's fine. 157 00:08:20,200 --> 00:08:22,500 [no audio] 158 00:08:22,500 --> 00:08:24,300 So I am going to provide '/etc'. 159 00:08:24,800 --> 00:08:26,800 Now your new working directory is '/etc'. 160 00:08:26,800 --> 00:08:29,500 Let me rerun, and I'm going to provide '/etc', 161 00:08:29,500 --> 00:08:33,500 something like 'xyz'. Now see that, "Given path is not a valid path. 162 00:08:33,500 --> 00:08:38,600 so can't change working directory". Now, let me provide '/etc, 163 00:08:40,000 --> 00:08:42,700 something like 'httpd', then 'conf'. 164 00:08:44,000 --> 00:08:46,000 "Sorry you don't have access for given path. 165 00:08:46,100 --> 00:08:47,799 So can't change working directory". 166 00:08:48,700 --> 00:08:52,900 So guys if you observe clearly, changing your working directory 167 00:08:52,900 --> 00:08:56,299 is very simple code, 'os.chdir'. 168 00:08:56,299 --> 00:09:00,100 But while changing in your real time, you will get some exceptions, 169 00:09:00,100 --> 00:09:01,900 so you have to be ready to handle them. 170 00:09:02,400 --> 00:09:05,000 So now we handle them in our code. 171 00:09:05,600 --> 00:09:09,500 So this is simply your simple Python Script, but now I 172 00:09:09,500 --> 00:09:12,100 want to implement same script in terms of functions. 173 00:09:12,300 --> 00:09:16,200 Anyway, I am not going to write deep functions, simply 174 00:09:16,200 --> 00:09:17,600 I'm writing very simple function. 175 00:09:18,800 --> 00:09:22,100 So guys, we know that it's better to start your code 176 00:09:22,100 --> 00:09:24,800 always with 'if __name'. 177 00:09:25,300 --> 00:09:29,500 The reason is, whenever if someone is going to import your 178 00:09:29,500 --> 00:09:33,800 code at that time you can hide your main logic, they can use 179 00:09:33,800 --> 00:09:36,600 only your 'main' function. So that's it. 180 00:09:37,400 --> 00:09:38,600 Now, let me simply write, 181 00:09:39,200 --> 00:09:41,500 anyway, just import your modules at very first 182 00:09:41,500 --> 00:09:45,300 line, right, then start with 'if __', then 183 00:09:45,400 --> 00:09:46,700 write your 'main' function. 184 00:09:47,000 --> 00:09:50,700 So as of now, I'm not going to write any number of functions, 185 00:09:50,700 --> 00:09:52,200 I am going to write only one function. 186 00:09:52,400 --> 00:09:55,200 So inside of this function just I am writing this logic. That's it. 187 00:09:56,800 --> 00:09:58,700 Just for your understanding purpose. That's it. 188 00:09:59,900 --> 00:10:01,500 Now, let me provide indentations 189 00:10:01,500 --> 00:10:03,000 otherwise you will get an error, 190 00:10:03,000 --> 00:10:07,000 [no audio] 191 00:10:07,000 --> 00:10:08,800 I mean you'll get an indentation error, right? 192 00:10:08,800 --> 00:10:17,300 [no audio] 193 00:10:17,300 --> 00:10:18,300 That's it. 194 00:10:19,300 --> 00:10:20,700 So now your function is over. 195 00:10:21,300 --> 00:10:24,600 Anyway, as of now I'm not expecting anything from your 'main' 196 00:10:24,600 --> 00:10:27,300 function to your 'if' condition that's why simply at the end 197 00:10:27,300 --> 00:10:29,900 I am writing 'return None'. 198 00:10:30,500 --> 00:10:33,600 That's it. So I commented your previous code. Same code 199 00:10:33,600 --> 00:10:35,800 just I have it inside of your 'main' function. That's it. 200 00:10:37,400 --> 00:10:41,100 Right. So guys be clear, whenever if you're trying to write your 201 00:10:41,100 --> 00:10:44,100 code it's always better to start with your 'if' condition, 202 00:10:44,100 --> 00:10:46,500 and then some, not only 'main', you can take any function name, 203 00:10:46,500 --> 00:10:50,300 but generally we'll take 'main'. Just write that 'main' function 204 00:10:50,300 --> 00:10:51,300 here. That's it. 205 00:10:51,500 --> 00:10:54,600 Then inside that you write your logic. That's fine. 206 00:10:55,300 --> 00:10:56,300 Now, let me run it. 207 00:10:56,700 --> 00:10:59,500 So before going to run let me provide shebang line as well. 208 00:10:59,700 --> 00:11:01,800 So this is my Python location. 209 00:11:02,200 --> 00:11:05,400 So I want to provide this at very first line in my code. 210 00:11:05,400 --> 00:11:09,700 [no audio] 211 00:11:09,700 --> 00:11:14,600 That's it. Now I am going to provide execution permissions for my script 212 00:11:14,800 --> 00:11:17,600 Now I can run my script in this way. Now I am providing 213 00:11:17,600 --> 00:11:19,900 [no audio] 214 00:11:19,900 --> 00:11:23,700 'Permission denied path'. So I have some path on my host for 215 00:11:23,700 --> 00:11:25,500 which I don't have access. Now see that. 216 00:11:26,900 --> 00:11:32,300 Okay. So guys this is the simple way to handle your script 217 00:11:32,400 --> 00:11:35,200 with exceptions whenever if you want to change your working 218 00:11:35,200 --> 00:11:38,000 directory through your Python Script, okay. 219 00:11:39,000 --> 00:11:40,900 Okay guys, thank you for watching this video. 220 00:11:40,900 --> 00:11:47,119 [no audio]