 Hello everybody. Today we're going to be creating an automatic file sorter for your files and file Explorer. Now out of all the projects that we've done in this series so far, I think this one might be the most difficult, but I also think this one is the most cool because it has some real life applications. So without further ado, let's take a look at some files that we have right down here in my file Explorer. So I have this beautiful picture of Rosie right here. This is a PNG file. I have a CSV file and a text file, and I want to sort all of them into their own folders depending on what kind of file it is. So if I go right in here and I click on this one, I go to properties, I can see that this is a PNG file. If I go into this one, I don't need to, but if I go into this one, it's a CSV file. And of course, this one is a text file. So I want three separate folders in here. And I want them to automatically go into those folders without me having to drag and drop and going and clicking. Now, we only have four files here, but imagine if we have thousands of files, how much time that could save us. So let's get out of here. And let's start writing our code. So we're going to say import os comma. And then we're going to say shut il. Now os obviously stands for operating system, shut il. I don't know what it's actually supposed to stand for, but what it will allow us to do is do some high level operations on our files in file explorer. So we're going to go ahead and import those. And now that we have those imported, something that's going to be very important for us to have throughout this whole thing. And this is anytime I'm working with like directories or something like this, we want to get this path down. So I'm going to go ahead and copy this path. And we're just going to say path is equal to and we'll do this right here. So let's run this. And I need to put an R right here to make this a raw text. So when you don't have the R, it's going to read in these, you know, these backslashes and these colons and different stuff. If we do R, it's just going to read it in as the raw string. And that's what we want. So here's what we need to do. There's a few different things that have to happen when we are writing this out. One thing is, is we need to go in here and we need to see this path and we need to see, are there folders in here already? If not, we need to create a folder. So that's one of the first things that we need to do. The next thing that we need is it needs to check each of these files individually, identify what kind of file it is and then put it into the correct folder. So we have to create the folder, then check these and then place it into the correct folder. So let's go right out of here. So what we're going to start doing is we're going to start working with these paths and these directories. And some of these things you may never have seen before, but that's okay. I'll try to explain it as I go through. So the first thing that we're going to write is OS dot list directories. And what this is actually going to do is show us all the files in there. We're going to say path. So it should show us all the files within path. And so here are results. So we have the data professional results, big text file, our image and our other image. So this is actually showing us what files are in that path. And that's super important because we're probably going to have to loop through this in some way later. I wrote this all out before. So I kind of remember, but I'm doing this off top of my head. So I guarantee you throughout this, I'll make some mistakes. But what we now need to do is we need to create folders or check if there's a folder and create it if it isn't there. That's the next step that we need to take. So let's go right down here. And we want to check if this path exists already. So if that folder already exists, so we're going to say OS dot path dot exists. So this is going to check, does this path, just like this path up here, does it already exist? And then we're going to do an open parentheses, we'll say path. So that's our path. Now we need to add a folder name to this. We could hard code it. So we could do plus, we could say CSV files. And that could work. So it's a does this path already exist. And we can try running this, and it's going to say false. So this doesn't already exist. But the thing is, is we need to create three separate paths. So we could do this by just hard coding it in by saying CSV files, image files, and text files, or we can just put this on a list and loop through it. I think it's just going to be easier to do that. Or I don't know, visually, it's going to be easier. So we'll do folder underscore names. And we'll say is equal to, and we'll create a list. So I think I want to call it CSV files, image files or PNG files, whatever you want to write. And then we'll do text files, do text files. And then we can go right down here, a little for loop. I think we'll do, actually, let's write folder underscore names. Then we can put something like pop up, let's write loop. Why not? So a little trick for the for loop is going to say for, and we'll say loop in and we'll just do a range because we want it to basically go through here. We don't want it to actually give us these file names, we just wanted to count zero one and two. So if we do range from zero to two zero zero one two, that should work. If we do this, then when it loops through, it's going to call folder name and say zero, which will be CSV files, image files and text files. So let's, yeah, I need a colon, let's run through this really quickly. I shouldn't do anything. But what we can do now is we can say, okay, if this does not exist, what we can do is actually create it. So we'll say, if not, so if this does not exist, then what we're going to do is take this and we'll say os.make directory and then we'll do just like that. I think it's make directory s. I think that's correct. So let's test this out really quickly. Let's see if this works. And invalid syntax, I need a colon. Okay, so I just ran this. Let's see if it did actually make those folders. Let's refresh it. And it didn't. So let's just print this off. So if not, let's just print. Let's see, does this actually work? Let's do if, okay. Ah, okay. So I think I know what might be happening. I think it's giving us, it may actually be, let's check this really quick. Go to Python tutorials. Oh no. I think it's creating, yeah, it's creating these Python tutorial images right here. Whoops. Okay, so I just figured it out. Let's go back into Python tutorials. Don't take a look at any of those notebooks. Those are secret. We were creating them in the wrong place. And that's because of this right here. We need a backslash, so we need to actually include a backslash right here in this path. We didn't have that. ELY scanning string literal. Okay. So this backslash could cause an issue. Let's see if I can do forward slashes on all these. Just stick with me, guys. I might cut this out. I might not. We'll see if this is important. It's going to keep talking while we're doing it. Um, let's run this. Okay. So now that we're doing these forward slashes, we're still checking. Let's make sure we can still check those files. Good. Now when we loop through this, I'm not going to, well, yeah, I can print it off. It doesn't matter. I'm going to print it and we'll see if that name works. And then we're also going to, um, well, I said if. So if it exists and make it, no, no, no. So if not, I think the not did make sense. We just weren't sure we had to do some checking. So if it exists, then we're going to create it and we'll keep the print in there because it doesn't really matter. So it's going to create the CSV and image, but didn't create the text. Let's see. Okay. Let's, uh, I don't know why this would work, but let's run it. Okay. So I think I just had the wrong range. So now we have our images all or we have our folders, all three folders. Now we need to write a script that will read in these and check and see what kind of file it is and place it into the correct folder. So let's come right down here and let's see what we need to do. So now I think we need to use this right here. I think we need to loop through this to be able to check each one. So we need to name this. So we'll just do, um, file underscore name is equal to run that. So now we have this file name. Um, and what we can do is loop through this. So let's say, let's say for file in file name. So we're going to loop through this. Now when it goes through, it needs to check the, it's going to check the file path. And in the file path, it'll say dot txt dot csv. So let's say, um, if I think it should be dot csv, let's test it on this one. But if csv is in file name or actually it's file. So if it's in file and not in and not not in, but if it's also not in this, I believe, because we're going to check, we're going to check each of those folders. So we're going to loop through, and it's going to check and see if the csv, so if that string is in the file, then what we want to do is check it that it's also not in here. That's actually just the folder we also need. Also, we're not doing that for loop anymore. Um, okay, I'm sorry, I'm talking this through. I'm figuring it out as I go, because I may have forgotten some of this. So we're going to say this, that's the csv files. So we need to check this one. Let's do it like this. Oops. Okay. So it's going to check to see if csv files, and I think it needs that in between it. So it's going to say the path. So there's our path plus slash csv files. Actually, no, it needs to be like this, because we're going to check that. Then I got it. All right, I figured it out now. Then we're going to check if this file is in there. Yeah, so that's right. So it says if the csv is in the file, which is right, where am I looking? Oh, file name. So if it's in that list of the actual files, which is all of these, if we find csv in any of these files, and it's not already in here, so it's going to say path plus csv files. Did I say files? Yes, csv files plus file. Okay, that all looks correct. So if it's not in there, we're going to use shuttle.move. Now this is how we actually move the file. It gives us the ability to move what we want. Then we'll say move. We need to take it from our initial path to our new path. So we're going to specify, we'll separate by comma, we need to specify its original path, which it should just be this without this. I think it should be file path because this is where it is now. It's in the font this path with that file name. Then we need to say we want to move it to here. That is what we want to do. Yeah, so let's check it with just this one and see if it works. Okay, it ran through it. Let's go check. Now that csv file is gone. Perfect. That is exactly what we want it to happen. Now we can just recreate this for both our PNG files or image files and our text files. So we'll say LF and LF and let's do PNG. Then we'll do image files and image files because again, we're just doing the exact same thing. I can do text files. The next one's going to be text files. So this one's going to check for txt. Now do we need anything else? We'll just say else and we'll print off print. This file type is not included or if there's multiple files, we'll say there are files in this path that were not moved. Okay, so if we run through this, it's going to catch our csv, catch our PNG, catch our text and if not, we'll say there are files in this path that were not moved. Exclamation point. All right, now let's run through this. That's because if LF, LF, LF and then it's going to this L statement. I don't know. Let's circle back around to that in a second. All of them were moved properly. That's really good. Really quickly, I'll check and see. I'm going to take that out for now. So I'm just going to run it. We may or may not go back to that, but let's check and see if everything works properly. So let's go into the csv file and we have our csv file. Go into image files and we have our images and let's go into our text file and there are our text files. Now, is there anything else that we need to do? I don't believe so, but what I can do is I can take all this, I can include it in here and I'm going to basically restart it just to see if it works properly from scratch, right? I just want to make sure that I didn't miss anything and we'll delete these. So I'm just going to rerun everything. We imported, we created our path. These are our file names and then when we run this, it should take our folder names, check through them. If they aren't already created, it's going to create it. Don't need it to print. So let's get rid of that. Then for the file within our file names, and it checks each one, we check if there's a csv and if it's already in that file, if it's already in that folder, I mean, if it's in that folder, then it doesn't do anything. But if it isn't, so and not, it's not in there, it is going to move it to that location. So it's going to check csv, png and text. I think everything should work properly. Let's run this. And it looks like it's working. Good, good, good. And perfect. It worked exactly how I'd hoped. That's great. So this is the automatic file sorter in file explorer project. You can go even a step further. So I had to come in here and manually run this. You can go a step further and put a timer on this where it automatically does this maybe every hour, every day, every 30 minutes. You can run this in your background, especially if you create like an execution for this. You can run this in your background. If you are curious on how to do that, I think I did something similar to that in my web scraping project. My Amazon web scraping project, if you want to go check that one out, but we're not going to do it in this project. This is all I wanted to show you how to do. So I hope that this was helpful. I hope that this project was interesting and that you liked it. I hope that you learned something. And so if you did, be sure to like and subscribe below and I will see you in the next video.