I want to talk quickly about file paths, because for some people, this might be a new concept.
Up until now, I wrote let's say something like sad pie or sad text, and I was able to run this code
because when I ran it in my terminal or command prompt, they 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 sad and test text.
But what happens if this wasn't the case?
We can actually jump back a directory by saying Dot, dot.
So if we go back, my present working directory is now user anechoic.
So I'm not on the desktop anymore so that if I try and run Python three and then say script.
Dot pie.
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 app.
So now I have an app folder.
I can move.
This SAT 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 sad text.
So let's say Python three.
Script up by.
Well, if I go back I've appended sad dot 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 error.
Sad text doesn't exist because it checks the desktop and on the desktop we have nothing.
We don't have sad text.
So how can we get around this?
Well, I simply say that I want to go into the directory app slash sad dot text so that if I run this.
Now it's a 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.
Print.
Read here.
If I run this.
All right, it's working.
Now I'm able to change the file path.
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 this 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 what your present 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 paths.
Now this is called a relative path.
So relative to where I am, where I'm running the script from, I am going to go from here into the
app, into sad text.
I could have also copied this entire file path.
Like this.
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.
Dot slash 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 error.
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
sad 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 paths 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 3.4 called
Path Lib.
It's an object oriented file system 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, let's 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, Path Lib 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 Lib
is going to take care of the file paths 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 you in the next one.
Bye bye.