I want to talk quickly about file parts, because for some people, this might be a new concept.
Up until now I wrote, let's say something like Sa'adat Pie or sad dot text.
And I was able to run this code because when I ran it in my terminal or command prompt, there were
all in the same folder on my desktop.
If I do present working directory on a Mac, it shows me where I'm currently at, which is the desktop.
So I was able to run it like this because my script file was in the same folder as my SAT and test text.
But what happens if this wasn't the case?
We can actually jump back a directory by saying KDDI, dot, dot.
So if we go back, my present working directory is now user and I go, so I'm not on the desktop anymore,
so that if I try and run Python three and then say script.
Well, can't find the file, right, so let's go back to desktop.
And try to figure out this problem, I'm going to for now make a directory.
And call it, let's say.
Well, a very generic folder or let's just call it at.
So now I have an app folder.
I can move.
This sad text into the app folder now, so if I grab the sad text and move it to the app folder.
If I go back now and.
Try to access Saddat text, so let's say Python three.
Script up by.
Well, if I go back, I've appended Sadad text, so it's actually creating it for me.
So instead, let's actually remove sad text because we actually have one already in here in the app
instead of appending it.
Let's just do a read.
If I try to read now, I get a file not found.
Er Saddat text doesn't exist because it checks the desktop and on the desktop we have nothing.
We don't have said that text.
So how can we get around this.
Well I simply say that I want to go into the directory app Slash said dot text so that if I run this.
Now it's it it's reading it, but it's an unsupported operation, so let's just turn this into a read.
And we don't need any of this, so let's just.
Read here if I run this.
All right, it's working now I'm able to change the Forbath.
Now, if you're on a non Unix based system, that is, if you're not on a Mac or Linux and you're on
Windows most of the time instead of the slash, it's a back slash like this.
So Windows and Unix systems have different file paths and file system in place.
So this is something that you can read up on depending on your file path and why your prison working
directory is you'll have a better idea of how it should look on windows.
It's usually something like C and then backslash and then you'll have different file path.
Now this is called a relative path.
So relative to where I am, where I'm writing the script from, I am going to go from here into the
app, into Sadaa text.
I could have also copied this entire file path.
And this is called an absolute path, so if I run this.
This still works because I'm giving an absolute path, it's saying, hey, on this computer, this exists
from the root user all the way into here.
So this is another way of doing it.
Most of the time you'll see relative because it's a lot simpler.
Finally, you might sometimes see something like a dot slash dots simply means from the current folder.
So if I run this, it's still going to work.
It's going to say, hey, from the current folder, go to app folder, then go to the text folder.
If I do dot, dot, slash like this, if I run this, I'll get an air dot dot means back a folder.
So instead of our present working directory being desktop because we did dot dot will actually go into
my user account and it's going to try and find an ugly slash app.
Slash said text which is not going to find.
Anyway, this is a bit of a tangent, but I want to let you know that these file parts are important
and it's really dependent on a what operating system you're using, like Mac or Windows and also where
you're calling the file from.
Let's continue our discussion of files in the next video.
One quick note that I forgot to mention, there's a really useful built in module in Python and you
can see here it's new in version three point four called Path Lab. It's an object oriented filesystem
path.
Now, the main thing is you can see how to use it through the documentation.
But the key thing is that if you're building a program that, lets say reads something from the file
system, like a file.
And this program can be used both on Windows and Mac and Linux systems, you want a way to make sure
that it works on both, right?
Because we saw how Mac and Windows have different file system with different slashes.
Well, Pat, Leyb Library actually works with both.
You can see over here if you want to manipulate Windows path on a Unix machine or vice versa, this
is going to work for you.
And it's a good way for you to create software that is compatible with both systems because Path Live
is going to take care of the file pass for you based on what machine is running your code.
Now, this is something you can read on your own and experiment, but I wanted to let you know that
this does exist and it is a useful library for you to have in the back of your pocket.
That's it for now.
I'll see in the next one.
Bye bye.