[no audio]
Friends here we are going to
implement one simple Python Script that is,
simply how to change current working directory. See changing
current working directory with Python is very easy.
Let me enter into my Python 3 terminal, right, and I am going
to import 'os' module.
Let me show you that, 'os.', current working directory
where we are.
We know we have 'os.getcwd' syntax. Already
we have seen all these syntaxes in our previous videos, right?
Fine. Now suppose
I want to move or change my current working directory to
something, some location
I am giving, some path
I am giving. Now it has been executed. Now
let me check it where we are now, 'getcwd',
under 'root' it is see.
So if you write these three lines, right, then your script is over.
But sometimes while changing your directory you will get
some exceptions.
Let me show you them. Suppose
what I am doing is, 'chdir("/etc/")'.
I will do one thing first.
[no audio]
So let me move into 'tmp', and let me create
'hi.txt', and let me create directory.
[no audio]
Fine. Now, I am entering into my 'python3'.
Right. Let's make sure this is Python 3 syntaxes.
So you have small differences with your Python 3,
I mean compared to Python 2, right. Fine. First
let me import 'os' module. Then 'os.getcwd', right?
Then I am changing into suppose
"/tmp".
[no audio]
Now, what is your current working directory?
Fine. Now
anyway we are in '/tmp',
but just assume that you are in any location but I am trying
to provide '/tmp',
and then 'hi.txt'.
So 'hi.txt' is a file, right?
We can't move into that. In case you provide by mistake that
as the path to change, right, through your Python,
then you are going to get an exception.
What is that exception?
'NotADirectory' error, right.
Just make a note of this.
So that finally we'll create complete script.
So this is one of the exception you are going to get. When
you are going to get that,
'NotADirectory' exception.
Now, what I am doing is, 'os.chdir("/tmp/hello.txt").
[no audio]
See that. Now what you are getting, 'FileNotFound' error because
actually whatever the path you are providing, that is not
present on your host. In that case you are going to get
exception in this way.
Right. Now, let me make a note on this as well.
[no audio]
Then let me move into one more directory.
So suppose I have '/etc/httpd', there
I have something file called 'conf'.
Now see that. What is the exception you are getting.
'PermissionError', so 'Permission denied'. Actually have a path but you
don't have permission to enter into that directory, right?
So changing directory, working directory is very simple,
simply 'os.chdir'.
But while working in your real time you will get these type
of exceptions. Then whenever if you are writing a Python Script,
it should work in all scenarios.
That's why you need to think about all possibility exceptions.
These are the possible exceptions you have
for your 'chdir'.
Now, you need to write a Python Script, right?
So what I want to do is first I need to 'import os'. First
I am going to implement this script in terms of, I mean without
functions. Then after that I will show you in terms of simple
function, so that you will get practice with your functions as well.
Okay. Fine. 'import os'. So I am going to read a required path
from the user. So 'req_path =', just use 'input' syntax.
So, "Enter your
change directory", "Enter path to change directory".
[no audio]
That's it. Right. Just for user understanding purpose
we are giving this 'print' message.
So before going to change, let me print simply,
[no audio]
"The current working directory is", so you know, how we can
get with 'os', 'os.getcwd'.
Then, now guys see you're going to get an exception, right,
so you can easily handle with the 'try' and 'except' blocks.
So what I am doing is, 'try:'
[no audio]
'try:', so what I am trying is 'os.chdir
(req_path)'. So you are going to read the path from the
input side, right. Now in case if it is works perfectly, then
I can 'print', simply, "Now your new working directory is", then
I can say simply 'os.getcwd()'.
But while changing your path, while changing your directory you're
going to get some exceptions, right.
We have already taken those exceptions. Now in case very first
chance let me take this one.
The given path is not existing then, then you are going to
get this exception,
then I am handling this, 'except', in case 'FileNotFoundError',
then I can say simply,
"Please verify
[no audio]
given path",
or "Given path is not a valid path".
[no audio]
"Given path is not a valid path". That's it.
or sometimes you're providing valid path
but you are going to provide file path, then 'except
NotADirectory' exception. Then I can say 'print("
[no audio]
Given path is a file path.
So can't change
[no audio]
working directory")'. So same information if you want to write
you can also write in your
'FileNotFoundError' also. That's it.
[no audio]
Right. Then you have one more exception. Even though if you
provide valid path valid directory path, sometimes you
may not have access to enter into that directory, then
'PermissionError', then print simply
"Sorry,
[no audio]
you don't have
[no audio]
access to change".
Okay, "You don't have access to",
"access for the given
path. So can't change", you can write any message, whatever you want.
That's it. Other than this, in case if you get any other,
any new exception, just to handle that exception, 'except
Exception as e', let me print that exception. In case if you
get any new exceptions, then you can handle them,
you can see them here,
so that in future you can add them, right? That's it.
Right. Now let me exit from here, and I am going to write a
simple Python Script, 'change_directory.py'.
[no audio]
That's it.
Now, let me save it and let me run this with 'python3', so I didn't
provide shebang line.
If you want you can provide and directly you can write, by giving
execution permission
you can run './'.
Okay, something error.
I have given,
where? Yeah, two quotations. Let me remove one quotation.
Now, it's fine.
[no audio]
So I am going to provide '/etc'.
Now your new working directory is '/etc'.
Let me rerun, and I'm going to provide '/etc',
something like 'xyz'. Now see that, "Given path is not a valid path.
so can't change working directory". Now, let me provide '/etc,
something like 'httpd', then 'conf'.
"Sorry you don't have access for given path.
So can't change working directory".
So guys if you observe clearly, changing your working directory
is very simple code, 'os.chdir'.
But while changing in your real time, you will get some exceptions,
so you have to be ready to handle them.
So now we handle them in our code.
So this is simply your simple Python Script, but now I
want to implement same script in terms of functions.
Anyway, I am not going to write deep functions, simply
I'm writing very simple function.
So guys, we know that it's better to start your code
always with 'if __name'.
The reason is, whenever if someone is going to import your
code at that time you can hide your main logic, they can use
only your 'main' function. So that's it.
Now, let me simply write,
anyway, just import your modules at very first
line, right, then start with 'if __', then
write your 'main' function.
So as of now, I'm not going to write any number of functions,
I am going to write only one function.
So inside of this function just I am writing this logic. That's it.
Just for your understanding purpose. That's it.
Now, let me provide indentations
otherwise you will get an error,
[no audio]
I mean you'll get an indentation error, right?
[no audio]
That's it.
So now your function is over.
Anyway, as of now I'm not expecting anything from your 'main'
function to your 'if' condition that's why simply at the end
I am writing 'return None'.
That's it. So I commented your previous code. Same code
just I have it inside of your 'main' function. That's it.
Right. So guys be clear, whenever if you're trying to write your
code it's always better to start with your 'if' condition,
and then some, not only 'main', you can take any function name,
but generally we'll take 'main'. Just write that 'main' function
here. That's it.
Then inside that you write your logic. That's fine.
Now, let me run it.
So before going to run let me provide shebang line as well.
So this is my Python location.
So I want to provide this at very first line in my code.
[no audio]
That's it. Now I am going to provide execution permissions for my script
Now I can run my script in this way. Now I am providing
[no audio]
'Permission denied path'. So I have some path on my host for
which I don't have access. Now see that.
Okay. So guys this is the simple way to handle your script
with exceptions whenever if you want to change your working
directory through your Python Script, okay.
Okay guys, thank you for watching this video.
[no audio]