 Welcome back, everyone. This is Brian. We're going to talk about navigating folders in Python 3. Now Before we begin spoiler alert, there are multiple ways to do something. And this is true in every programming language on every computer operating system. Basically, as computers evolve, so do the programming languages and platforms that people use to create programs. They get infinitely more complex behind the scenes and infinitely easier to use up front. But they also hide a lot of that complexity, which leads to a lot of confusion, especially newbies, because they say, what's the best way to do something? The short answer is there isn't one. Use what works for you. We're going to cover a few situations, but you're going to see people even in the comments down below say, why are you using that way? Use this other way instead. Again, refer back to the golden rule. Use what works for you. Let's dive in and take a look. First things first, what we're going to do is import a module, you may be going now, wait a minute, I thought we're going to work with navigating folders. We'll understand that folders are part of the file system, and it can get very, very complex. And a lot of this is baked right into the core of Python, but we need to work with someone else's code. And some of these, the ones we're going to work with are already baked in the Python, we just need to tell Python we want to use them. So we're going to import the OS module. In case you're wondering what a module is, it is a group of code that we can use. Someone else has already written this code, we can just simply use it however we want. So first things first, import OS, so we're importing a module. Once that module is imported, we can start working with it. So I'm going to just make a variable called D, shorthand for directory, I'm going to say OS dot get current working directory. In case you're wondering what that is, every single application runs inside of a directory, every single one of them. There are a file on your system somewhere. That file lives inside of a directory. If you're on like a Linux or Unix or Mac, it's in what's called root, which looks like that's just a slash. If you're in Windows land, it's going to look something like that. Now, that is the root of the OS, but chances are you're not actually in the root, you're buried down deep in the OS somewhere. So let's figure out where we're actually at. So I'm going to say print. And let's go ahead and format that. And I'm going to say current. We just want to know where the heck we are in the OS. Sometimes this could be like, you know, navigating the deep blue sea or something. We just don't know where we are. So we're going to get that current working directory, or we could just use our variable, which we've already pulled out. Now I have to put this is the current directory does not mean that every single program runs in this directory. Just means what our program is running in all the other programs are running in different directories or even in the same directory. It can be either or it's super confusing. All right, on my little virtual machine, we are currently in slash home slash root shell, just the account that I'm on here slash code slash Python three. So you can see we're not in the root, we are in a sub folder, several levels down. So let's say just for example, we don't really like this folder. We just want to work with a different folder just to see if we can do it. So let's go ahead and change folders. So I'm gonna say OS dot ch dur, which stands for change dur, change directory. So get CWD is get the current working directory, we're going to ch dur or change directory seems a little confusing to get the terminology down. Now you can actually hard code this like let's say we could home Bob's files, something something, or if you're on Windows, it would be like a CWD, you know, whatever you wanted, or you can use what's called the current path, which means you don't have any sort of pathing in there. Again, a little bit advanced and I'm just going to do dot dot. If you're wondering what that means that dot dot. It actually stands for your current directory and its parents. So we're actually going up to levels sounds a little confusing. But let's take a look here. I'm going to just Grab some code here. We're going to print the current and we're going to say OS get current working directory. Notice how we're not reusing D and I'm going to Just grab that for future reference here. Let's run this. And we've gone up a notch. So we were in Python three now we're in code. So we just simply jumped up to the parent. So when you see dot dot, that's really all we're doing is jumping up a level. Now we're going to reuse this variable here and I'm going to say OS.chdir. And we're going to jump back to that string representation of that directory. Grab that, paste it. Clear this out just for good measure and run. So we went from Python three up to the parent and then back to Python three. So Python three up to the parent and back to Python three Navigating the folder structures is very, very simple. If you know the path, it's just ridiculous simple, but you just handed a string and boom, it just goes. Now that's assuming it actually exists. We're going to cover what to do if it doesn't exist in a future video, but Just know that if you try to give it something that doesn't exist, you're going to have a very bad time. Remember when I started this video, I basically said there are multiple ways to do the same thing. And we're going to start looking at some of those. The first one is list dir and this is probably the easiest way to do it. So I'm going to say for F in OS, remember that import that we added. And we're going to anybody drumroll, list, there you go. This is a function so we have to have our parentheses. And we can specify Director, we can use the current. Now if we just print this out, let's just go ahead and see what this does here. Not a whole lot here, it's just got one file plus this little dot vs code dot vs code is this folder right here, which has another file in it. So we're getting folders and files. And if you've got a lot of files in your directory, you're going to see them all. Interesting. So how do we actually determine what each one is. And let's pretty this up a little bit. Notice how this is just Python three dash 22, just the name of the script, but we don't get the full path. Let's go ahead and say print. Let's format that path. And this is where people in the comments below are going to start arguing with each other. I'm going to say OS path. And I want to get the abs or the absolute path. This is going to tell us the absolute path that file someone down below is probably going to say why aren't you using real file or something of that nature. Again, personal preference, this is what I want. Let's go ahead and run that. Oh, as no, oh, the sensitivity has come back to bite me. There we go. So now it is working as we expect path. And it's giving the full path, not just the name of the file. So that's the difference here is we've got the file name and then we've got the absolute path here. Let's go ahead and make this a little bit more complex. I'm going to say if and I want OS dot half. And let's make sure it is a directory. And we're just going to go ahead and print this out. And we can take the same pattern and just repeat it. So we're going to say if it's a directory, just print dir and then whatever it is. And then if it is a file, it's going to go ahead and print file and whatever it is. And let's add one more just for giggles is link. You're wondering what a link is. It's a special kind of symbol out there in file land, which basically says I'm not the right one. Go to this other location. It literally just points to another location. You can call them shortcuts or symbolic links. It really depends on your operating system. Notice how this will always return faults for Windows prior to 6.0 because Windows before 6.0 did not have symbolic links. And you may be wondering what's Windows 6. Did we ever get an advertisement for Windows 6? It's just the way Windows does versioning Microsoft's just kind of dumb sometimes. But basically, if you've got an older version of Windows, that's just not going to ever return true. Let's go ahead and save run. And you can see that our path it is a file. And then our path and half and it is a directory. So it's working as expected and we can now determine whether it's a file or directory and we can get the absolute path. So Lister, along with OS dot path is actually pretty complex and does most of what you may actually need it to do rewind the video a little bit. And you'll remember I said hey as computers get more complex, the code gets easier, but details get kind of murky. And this is a perfect example of this. Scander. This appears in Python 3.5 or higher. We've been using Lister. You may be asking yourself, why do we need Scander? Lister seems to do just about everything we'd want it to do. Well, Scander, the big selling point for this is it encapsulated in a class and it is faster, although I will just say it, unless you're doing some high encoding, you're really not going to see a massive performance boost and you may be scratching your head wondering why you've been working with it. But when it out, let's just dive in here. And I'm gonna say Scander. And let's go ahead and print this out. That's how I have E in there. When I run this, you're about to see something magical happen. Tada. It says. Dure entry and then the name. So what does this mean? This is a weird little syntax. Whenever you see something encapsulated in brackets like that. It means that Python is handing us a class and we're going to talk about classes in a future video. But right now, major leap of faith, a class is just simply a custom data type. It's really a blueprint for an object that someone else has defined. We're going to learn how to build our own classes and blueprints in the near future. But right now, all you really need to understand is we have a custom data type called a Dure entry. And we just say data types, we're talking about things like strings, numbers, lists and things like that. Dure entry is just another data type. Let's go ahead and work with this a little bit here. We're gonna say print. And this is what I mean by you get lost in the complexity a little bit here. We're kind of scratching our head figuring out what we would need to print out. So you have to actually go out and read the docs or watch the rest of this video. Let's go ahead and say name. And we're gonna say E dot. And we want the name. Go ahead and get the path here. So I'm gonna grab this. So the whole purpose of this is to make life a little bit easier, but at the same time, Scander does work faster than Lister. And it works now. Name path. Let's go ahead and run a little bit of an issue. Now, when I say run a little bit of an issue, we want to get the directory, the file and the symbolic link, the bulls, kind of how we did before. So I'm going to grab this. And this is where it's going to get a little mind bending here. So if I say E dot is Dure is file is symbolic link, you think it's pretty straightforward. You go, okay. Let's try this. But what we're doing under the hood is subtly creating an issue. And I'm wondering if anybody out there can really spot this. Let's go ahead and grab this name here. Now that we've done a little bit of surgery, hot quiz, can anybody spot the issue? Raise your hand. So what's going on here? Dure entry dot VS code. We said dot VS code is actually a directory. See, there it is right there. But it is printing out Dure file and link. Wait, it can't be all three at once. What's going on here is we're actually calling a function, or at least we should be. I wanted to demonstrate that because somebody out there is going to run into that issue where they forget to put the parentheses and then they have some weird results. Run that again. And you see now it works as expected, but it never actually threw any kind of error. That's the confusing part. So if you ever get some weird kind of results out of Scander, just make sure you got those parentheses there. Okay, well, let's go ahead and cover glob and I put glob multiple ways because so far we covered two other ways to do it. Blob represents a third way and blob, although it sounds gross, is actually really cool. In short, use what's right for you. We're covering glob because of this and I put the link here. Typical stack overflow question, which is how do I scan all folders and sub folders or a k a a recursive scan? And if you kind of wrap in your head around what a recursive scan is, we're in this Python three folder, we're going to jump up to the parent here and I want to know the contents of all of these folders. And if one of these folders has a folder in it, I want to go into that folder and so on and so on and we're just going to do a recursive scan. Now in some languages and frameworks, this is not for the faint of heart, but glob makes it ridiculously simple. So first thing we need to do is we need to import glob. I don't know why you'd want a glob. But anyways, I wish they picked a better name for that. So we're going to change our directory. And we're going to go up to that parent. So we're going to go a directory higher. Now I'm going to say there, I'm going to make a variable here. This is going to be OS. And we're going to get the parent or say get current working directory. Now we're going to actually work with glob. And I'm going to say for file name in glob. And glob is going to get really confusing. And we're not going to like deep dive into all of the parts of glob. But know that you can do a bunch of different things with glob or really just scratch on the surface. So we're going to say glob glob. Sounds like you're talking to a drunk person in a bar glob glob. Anyways, so glob, we want to give it a path name. We're going to have IntelliSense help us here. So path name I'm going to say is equal to our directory. And we're going to give it some wild cards here. We need to tell glob what to do. Going to say anything followed by anything. Whenever you see star, that's our wild card. And then we want recursive equals true. So we're telling glob, not just this directory, but all the directories inside of it. Go ahead and do your magic. Go ahead and print that out what formatted. I want to glob this out, like stuck in my head now glob. Anyways, you know, there was probably like a bunch of programmers sitting around a room going, we made this really awesome thing. What do we call it? Let's call it glob. And then they kind of took that seriously. There you go. So glob has gotten a lot of stuff. You can see I've got other code from other projects in there, including C++ files, make files, text files, other Python scripts that I've worked on, things like that. So glob has gone through and read the contents of all of those. Now, to kind of show that there are multiple ways to do this, let's go ahead and do it yet a different way. Okay, so let's say four. Let's go current path, folders, and files in OS, our good old buddy OS dot walk. And basically we're going to tell OS to go take a walk in our current directory. Now remember, dot dot is the parent dot is the current. So we're just going to say OS walk the current directory. And I'm going to go ahead and say for file in files. This is not so much a recursive scan, but it gives you a little bit of detail here. Let's say go ahead print. Let's go ahead and work with these almost a OS dot path dot join. So what we're going to do is we're going to join all of this up. We want to know the current path and the file. And if you're kind of confused about where I got current path and file, it's right up here. So really what OS walk is doing is returning. I believe it's a tuple. And then we're just pulling those elements out of there and then using OS path join to join all that up and make it look something like this. Pretty interesting, pretty easy to understand. So to summarize, there are, well, multiple ways to do virtually the same thing. Each one has its own little pros and cons and little gotchas and different ways of doing it use what's right for you. This video covered how to change folders, Lister, Scander, glob, along with OS walk. And you're going to see all those out in other people's code. And we were talking about recursive and that has its own set of complexities, which we're also going to cover in future videos. This is the beginner's guide and we're just scratching the surface of what we can do. I hope you enjoyed this video. You can find the source code out on github.com. If you need additional help, myself and thousands of other developers are hanging out in the void realms Facebook group. This is a large group with lots of developers and we talk about everything technology related, not just the technology that you just watched. And if you want official training, I do develop courses out on udemy.com. This is official classroom style training. If you go out there and the course you're looking for is just simply not there, drop me a note, I'm either working on it or I will actually develop it. I will put a link down below for all three of those. And as always, help me help you smash that like and subscribe button. The more popular these videos become the more I'll create and publish out on YouTube. Thank you for watching.