>> So we need to work within
the file system when we're writing our Python code.
Said this comes up a lot,
when we're working with files.
Maybe we need to check what Directory we are in,
we want to get a list of the files in that directory, and so on.
So we're going to use the pathlib in the path library,
not OS.path,
OS.path was something we used to do with older versions of Python.
But with Python 3.6,
we now have a cleaner and
slightly faster method for
checking and talking to the file system to see what's going on.
So what I've got here in my code is you'll
see I've imported from the path library,
pathlib, sorry,
I've imported path from pathlib, the other way around.
One of the most common things we want to do is just say,
"What directory am I in now?"
Because I may need that to build
a file path or simply to check some information.
So CWD will return
the current working directory and will
print that out on the screens so you can see that.
Then you can also,
this is really, I love this little feature.
It's such a simple thing,
but it allows you to build a full path name.
So because I'm always mixing up forward slash and backslash,
and I can never remember when I'm building a path,
is it a backslash or a forward slash
and it depends what systems you're on,
and so I love the fact I can just use a method called joinpath,
and I give it the directory name,
and I give it the file name and it inserts the right slash for me.
So I don't have to remember backslash or forward slash.
This makes me happy.
It's the little things in life.
You have to appreciate them.
So this will simply insert
the appropriate slash for me between the file path,
the directory name, and the file name itself,
and I'll just print that on the screen
so you can see how that appears.
Another useful tool is
the ability to check and see if a file exists.
This comes up a lot of times when we're
writing code that's maybe going to be moving a file
from one place to another or before we write
code that's going to read a file,
it's a really good practice to
just check and see if the file exists first,
rather than trying to call an
open and having the open command fail,
it's much cleaner to say, "Hey, is the file there?"
If it is, great, let's open it and continue,
so that you have more controlled flow and cleaner error handling.
So it just a nicer way to handle things
than writing a bunch of try and catch statements.
So if we run this code here,
we go down here and execute it.
What you're going to see on the screen is you're going to,
and I'm going to make this a little
bigger so you can see a little better.
What you're going to see is here's that current working directory.
Yes, I have a very long set of subdirectories that I use
when I'm storing my demo files, keep track of everything,
and then you can see the full path, and you can see it's,
here's the full file directory name
and the appropriate slash added to that directory.
Does that file exist? Well, no.
I was just inventing
and pretending there was a file called new file,
it doesn't actually exist in this directory.
If it did, you'd see it listed over here in my list of files.
So it says false.
So some neat operations we
can do for manipulating the different paths.
Now, the other thing we often need to do
is navigate between different directories,
to see what files are in a directory and things like that.
So let's see how we can do that using the same awesome library.
So again, I'm probably going to start with
this my current working directory,
because you can actually see the file that I have here.
So it'll be easier for you to see it's working correctly.
I can actually test to see if I'm past something,
whether that value points
to a directory or file, which can be useful as well.
So if you know, you should be looking at a directory,
you can check that first,
or if you know you should be opening a file,
you can check to make sure it is a file,
and again, you can list the directory contents.
So if I run this code here,
what we're going to see, start with a nice clean screen.
So again, I'm just going to bring this up a bit,
so it's a little easier if you see what's happening,
especially with a list of file names, it gets a little bit long.
So what you can see is
the current working directory, is this a directory?
Yes. Because I asked it is the current working directory,
which directory it is? Is it a file?
No. So it returns false.
If you go in, you'll see a list of all the files
that I currently have inside that particularly directory.
So again, this is really handy because sometimes,
you might need to read all the files in a particular directory.
So this way you can get a list of the files and you can use
the is file or is directory to see if
that when it returns a value.
So let's say you have two subfolders and six files.
You only want to open the files.
You may not want to open the subfolders.
So this way, you can use that is directory to know,
hey, what in found in this directory,
was it a directory value like a subdirectory I need to
move into or is the file that I should be opening?
So these two things together give us a lot of
power for navigating across
folder structures and opening
all the files in particular folder, and so on.
The last one I want to show you here is
the options we have for just looking up information about a file.
In the next module, we're going to talk
about how to read and write to files,
so don't worry about that yet.
So if I have
a location and I want to know information about the file.
So a lot of times we end up writing code that has to
move maybe a file from one place to another,
or we have a file that has data in it,
which we're going to be using to do
some data science or to load into a database,
and we may want to make sure that file it has data in it.
I actually ran into a genuine a live issue where,
I was loading a data file into a database
and I accidentally deleted all of my code.
Basically said, delete the records that are currently there,
take the data file,
load that data file into the table to replace the data.
Something had gone wrong with the file transfer.
So I deleted all the records in
the table and then loaded an empty file.
So when I was done, there were no records in the database table.
So things like being able to check the size of
the file and stuff before you load it can be really
useful to notice things and
do good logging to explain why something is
working or might have failed to help
people figure out when they come back afterwards,
" Hey, why did our data load fail?"
These kinds of commands allow you to check
things along the way and help you
debug what might've gone wrong
when later on there's no data in that table.
So if we look at the code here,
I can do things like to create a path to that name.
So I get the working directory,
I get a file name by joining the path to the file name itself,
and then I can request like what's the file's name?
What's the file suffix.txt,.csv,
what folder is it in,
and statistics about it,
like it's size or when was it last modified and so on.
So if I run this code,
what you're going to see here is
you're going to see that the file name is demo.txt,
and that was the file we're pointing to here, demo.txt,
the suffixes.txt, it's in
the folder 06-file_system and the size is 11 kilobytes.
So anytime you're writing code that's going to
be reading files or writing to files,
before you open up that file,
take a look at some of these to check things first and make
sure that everything is the way it should
be before you start modifying those files.
On which note, maybe that's what we should look at next.