>> When you start working with code,
at some point you're going to have to
start interacting with the file system.
You're going to either need to read files or write to
files and figuring out
what directory you're in and issues like that.
So let's take a look at some of the Python commands and
libraries we can use to work with the file system itself.
Now, in the earlier days of Python,
we typically used os.path,
and that's operating system path.
So we would literally call the operating system and say,
what path are we in?
Now, the great thing is, in Python 3.6,
they actually added a new library,
pathlib, which includes a class called path.
That's a cleaner way and actually
slightly faster way to access
information about things like what directory I'm in?
What files are in that directory and so on,
because it doesn't require
accessing and calling the operating system itself.
So python 3.6 or higher,
you definitely want to be using path and path lead.
But if you're looking at older code from someone else,
just be aware you might see something called os.path.
But we're going to focus on going forward,
what we want to be doing as we move forward.
So it's the path libraries.
So we're going to import that library to be able to use it,
and once we've done that,
then we can simply use cwd for current working directory.
So if you want to retrieve what directory I'm in right
now, it's just path.cwd.
The other thing you commonly want to do
is we want to know a full path name.
So the name of the directory and that is slash or backslash,
and the file name.
So we want to include that in
the full name so you can actually build that.
There's this wonderful function called join path which will
actually build you the correct structure
of a directory name and the file name together.
Another common test we do,
because often before we write to a file or read a file,
we want to check if the file already exists because we might write
good code where we're checking
to see if file exists before we try and read it.
So we don't just get error messages causing things to crash.
It's much more elegant to say,
hey, let's check if the file exists.
If it doesn't, maybe we return our own error message.
So controlled error handling instead of having a function fail.
So another great feature is to be able to check and see
if a file exists using the Boolean variable.
So few features we have,
and if you do that, what you'll see come back is
you would see there's your current working directory there.
You can see the full path name
with the directory name and the file name.
You can see in this case,
I haven't actually created a file called new file.
So when I asked if that file
exists even though I've created a file path for it,
the file itself is not there,
so it returns a false.
Now, in directories, the other common thing we end up doing,
this comes up when we're maybe doing
data science and reading a set of files,
or we're just moving data from one place to another is
we might need to check things like go into a directory,
get a list of the contents of that directory,
get a list of the files, etc.
We can keep using the path library for that as well.
We can request the parent directory.
We can ask, is this a directory or a file?
By using, is directory true-false is file true-false.
For child in parent,
it's a fantastic one for just getting
a list of the contents of a particular directory.
So if I was to run that,
I might see something like if I pass it the working directory.
Well, the working directory is a directory name.
So is it a directory true?
Is it a file false?
What's contents of the current working directory?
It might be something like following a few code examples here.
Now, files is the other one we're going to be working with.
We're going to talk about reading and writing
to files in the next module.
So I'm just talking about how do I find out
information about what files I have first.
So don't worry, we're going to get to reading and
writing to files in the next module.
But just to find out information about your files.
Some of the things we can see
once you know that working directory,
you can do things like retrieve the file name,
retrieve the file suffix,
retrieve the folder name,
or even information like what size in it
or when was it last-modified?
So you might determine if maybe there was
a program that was supposed to modify the file in the background,
has that program completed before I go reading it and so on.
So if you were to run this code,
you'd see something like, Oh,
the file is demo.text and the extension is.txt.
It's in the folder file system and its size is say, 11 kilobytes.
So those are some of the neat things we can do.
Let's go try it out in some actual code.