Instructor: I wanna 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.py 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 cd dot dot.
So if we go back,
my present working directory is now user aneagoie.
So I'm not on the desktop anymore,
so that if I try and run python3
and then say script.py,
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 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 sad.txt,
so let's say python3 script.py.
Well, if I go back, I've appended sad.txt,
so it's actually creating it for me.
So instead, let's actually remove sad.txt
because we actually have one already in here, in the app.
Instead of appending it, let's just do a read.
If I try a read now, I get a FileNotFoundError.
sad.txt doesn't exist because it checks the desktop,
and on the desktop, we have nothing.
We don't have sad.txt.
So how can we get around this?
Well, I simply say
that I want to go into the directory app/sad.txt,
so that if I run this,
now 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 backslash, 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 a different file path.
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.txt.
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,
but 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,
we'll actually go into my user account,
and it's going to try and find aneagoie/app/sad.txt,
which it's 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 pathlib.
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, 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 systems with different slashes.
Well, pathlib library actually works with both.
You can see over here,
"If you want to manipulate Windows paths
"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 pathlib 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.