Welcome back.
So, I know you're itching to jump into
the project, at least I hope so, but there's one little gap in our file and
folder knowledge stack that we need to fill before we're ready.
You see, our program will need to ask the user to provide the path of the folder
they want to clean up, but thus far we've been building up file paths piece by
piece, starting with the home directory and then adding more components as we go.
Now, this is generally the preferred method, by the way, since it gives us
more flexibility to manipulate those paths programmatically, but we can't
really expect our users to open up a Python script and know which lines of
code to change to customize it for their purposes.
So, what we need instead is for the user
to simply supply a complete file path as a text string and then for our program to
be able to convert that to a path object so we can easily work with it programmatically.
But how do we go about this?
Well, it turns out it's pretty diagonal straightforward.
It doesn't even require a fancy new method, just the same path class we've
been importing this whole time.
So, let's take a quick pre -project
detour and see how this works.
Now, to make our goal a bit more
specific, what we're trying to do here is take a path like the one represented by
these breadcrumbs up top here and which I can convert to a text string by right
-clicking this last folder in the breadcrumbs and then choosing Copy
Address as Text and then convert that path, which is currently a text string,
into a path object that we can work with using the PathLib library.
So, speaking of that, I'll go ahead and assign this text string to a variable and
then I'll also import the path class from the PathLib module.
So, again, our goal here is to convert this text string we currently have stored
in a variable called p directly into a path object.
No Lego -like building up from components allowed, but one problem we run into,
which you may remember from earlier in the course, is that, in the case of
Windows file paths, the individual components are separated by backslashes
and it turns out that backslashes have a double meaning in Python.
They also function as what's known as escape characters.
This means that a backslash, in combination with certain other characters
immediately following it, can represent another kind of character entirely,
typically one that is difficult to simply type out at a text string.
Python is going to interpret that character sequence as something other
than a literal backslash followed by a literal letter T.
More specifically, that would be the tab character, but that's kind of beside the
point here.
Now, of course, you can escape the
backslashes themselves by preceding them with other backslashes.
Python is no longer looking at the backslash plus T combo as a tab character.
But is it honestly reasonable to expect our users to know how to do that, but
only if they're on a Windows operating system?
Probably not.
So how do we solve for this?
The answer is converting this string into
something called a raw string.
Literally, exactly as we typed it,
backslashes and all.
And converting a string to a raw string
is ludicrously simple.
We just throw the letter R before the
string definition, including any quotes in closing the string, like so.
And that easily, ta -da, we have a raw string.
But now you might be saying, how do we go
about converting this entire string, raw or not, into a path object?
Well, it turns out we can simply feed the path class itself, which we've already
imported, a complete path as a text string.
It will happily return a path object
based on that string.
So let's code that up.
So down below this line, I'll declare a variable called path, and I'll assign
that the result of passing our text string into the path class.
So in object -oriented terms, we're using the path class here as a constructor,
meaning that we're creating a path object from it.
So now that we have this in place, let's go ahead and test for that path's
existence, shall we?
And we just saw that this folder very
much does exist on my computer, so if I run this, we should see a message to the affirmative.
So let's go ahead and try that.
And it works, with none of those pesky
double backslashes required.
Now at this point, if you're a Mac or
Linux user, you might be wondering how all this will work with paths separated
by forward slashes, like they typically are on non -Windows machines.
Well, remember when I said path -lib is pretty smart?
So it turns out if I convert all the backslashes in my path here to forward
slashes, so let's copy this line, and then comment it out, and then on a new
line, I'll just replace all of these backslashes with forward slashes.
So now if I run this code again, it still works, meaning Python is still able to
correctly interpret the path, even though this string is technically using the
wrong separator for a path on my Windows machine.
So clearly, path -lib can handle string
paths of both flavors, and that's pretty cool.
But how does all this apply to our
project exactly?
Well, we're going to be asking our users
to supply our little program with a text string representing the path to the
target folder, kind of like the string we see here.
But of course, we can't expect the user to open our Python script and change the
code in any way, so instead we'll prompt them to type or copy and paste it.
So let's say input, using Python's input function, to which we can pass a prompt
describing what we want the user to enter.
So once again, I'll comment out our p
variable, and then I'll change that to be the result of a user's input prompted by
the input function.
So I'll say input, and then the prompt
will be please enter the target folder.
So now let's see what happens if we copy
our file path here, note that I'm only copying the path, the Windows one, not
the Python -y stuff we wouldn't expect our users to know, like the quotes or the
letter R at the beginning, and then rerun our code, and then down on the console,
after the prompt, I'll simply paste in that string and hit enter.
And somehow, this appears to have still worked, even though we didn't get the
chance to convert this input to a raw string first.
But how?
Well, you see, the input function
actually converts any text entered by a user to a raw string automatically, so
when we generate a text string in this way, or rather have someone else generate
it for us, we don't have to do a thing.
Pretty convenient, huh?
And the best news is, this technique should give you a nice little head start
on your project, which, by the way, I think it's high time we get back to.
So I'll step aside for now and let you get busy slinging Python code.
Then when you're done, I'll walk you through my solution in the next video.
See you then, and in the meantime, happy coding.