Speaker 1: What's up everyone? In this video I show you how to do file handling and file organization in Python. So I show you how to navigate to different directories, how to rename files, move them, copy them or remove files and directories. So these are all the functions we need in order to automate our file organization. And then I also show you one real life example. So I show you one automation script that I use in order to keep my desktop organized. So let's get started. First of all, let's learn how we can navigate to a different directory. And for this, we use the OS module. And first of all, we can print the current working directory by calling OS dot get CVD. And this will print this path. So this is where the main.py file is located. And now let's switch to a different directory. So here on the desktop, I have one folder that I called video files where I have a couple of files in it. So now I want to change into this directory. And I can do this by calling OS change stir. And then let's put in this path. So I want users, Patrick desktop, and then it's called video files. And now if I print the current working directory afterwards, then let's run this. And now we see we are in this directory. And now the next thing I want to do is to rename these files so that I can sort them according to the name. For example, I want the numbers to be in the beginning. And then there's also some spaces that I want to remove. So first of all, we can call OS dot list dir. And let's remove this print statement. And if we run this, then we get a list with all the files in this directory. So we can iterate over this. So we can say for file in OS list dir. And then I want to print the file. And if we run this, then we get all the different file names. And then we have this one that we don't even see here because it's starting with a dot. So I want to ignore this. So I can say if file equals equals this name, then I want to continue and otherwise print the file and then we should only get these three files. Now the next thing I want to do is to split the base name from the extension and then modify the name and put it back together with the extension afterwards. And the way to do this is saying name and x equals OS dot path dot split x and it needs the file name. And now if we print the name and then print the extension in a separate line and run this, then we see we now split it the extension from the file name. So now let's modify the file name and create a new one. And I want to have the number in the beginning, then I want Python course and then I want this title in the end. And I also want to get rid of the spaces. So the way to do this is for example, we can split this string at each dash and then get all the different elements. So we say split it equals name dot splits at a dash. So this is a function that we can use on a string. And if we print this, then we see this will be a list. So this is a list now with all the separate elements. So now the next thing I want to do is to get rid of spaces like here for each element. And we can do this with list comprehension. So we say split it equals s dot strip for s in split it. So for each string in this list, we call s dot strip. And now if we run this, then we no longer have spaces in here. And now let's create a new name. So we say new name equals and now we use an F string. And now we put in the different elements in curly braces and create our new name. So we say curly braces dash curly braces dash curly braces dash, and then we need one more. So in this very last field, we put in our extension. And here we want the number. So this is split it off element of index three, then we put this for the other ones as well. So this is split it with element one, this should be Python, then here split it to this is the course. And here we have the title. So this is split it zero. So now if we print the new name, then we see our name now looks like this. And now I want to have one more improvement. So here, I always want to have two digits in the beginning. And for this, I can call the handy C fill method and specify two as the argument. And now if we run this, then this is our final new file name. And now we only have to rename the file. And for this, we can call os dot rename, and then we need to put in the old file name. So this is the file, and then the new name. And now let's watch the right side. So now I will execute this. And you see it immediately changed all the file names and rename them. Now before we move on to moving files, I also want to show you how we can do this the modern Python way. So let's copy the old file names into this again. And we can also say we say from path lib, we want to import the path. And then we create a path object by saying f equals path. And here we pass in the file. And now we can say name equals f dot stem. And the extension is f dot suffix. So this will be the same. And then we say f dot rename to the new name. And now again, let's watch the right side and run this. And you see this work as well with the new syntax using the path lib module. Now the next thing I want to do is to move a file. And for this, let's get rid of all of this. And now the first thing I want to do is to create a new directory. And we can do this by calling path and then the specify a new folder name. And then we call make dir. And we use exists. Okay, equals true, then it will not raise an exception if this already exists. So now if we run this, then it created a new data directory, we could also do it the old way by saying if not OS path exists, and the name is data, then we call OS make dir data. And now here I deleted it again. And now if we comment this out and run this, then here it worked as well. So now we have to be careful because now if we print all the files, you will see that now we also have this data in lister. So it not only iterates over the files, but also over directories. So here we also have to say our file equals equals data now. And now in order to move a file, we import the sh util module. And then we simply call sh util dot move. And then we specify the source file. And now the destination is data. So this can take a whole file name, but also a directory name like so. And now if we run this, then you see it moved all the files into this directory, then let's learn how to copy files instead of moving them. So let's move them out again here. And now in order to copy them, we can use sh util dot copy. And if we run this, then you see it now created new copies in this directory. And now this has a new timestamp. So this is the timestamp of right now. And if you want to keep the old timestamps and all other meta tags, then you can use the copy to function. So if I remove these again, and now run the file, then you see now we have copies and we have the same old timestamp. So this is how you copy and it keeps all the metadata. And now as last thing, let's learn how we can delete a file and folder again. So for this, we can use os dot remove and then use a file name, we can also use os dot rm dir and then a folder name, for example data. But if we run this, then it will not work in our case, because now we get an error directory is not empty. So in order to remove a non empty directory, we can again use the SSH util module and then call rm tree. And now here data. So this will recursively delete a directory tree. And if we run this, then it worked. And now the whole directory is gone. And now the last thing I want to show you is this backup script that I use in order to keep my desktop organized. So as you can see, there are a lot of different files that get collected over time, for example, movies or audio files or images and a lot of screenshots. So here I defined all the possible file endings for audio files and the same for videos and images. And then I have helper functions that determine if a file is an audio file. And it works by using the split x function and then it checks if the extension is in the audio extensions. And the same for videos and images. And then I even have a helper function that checks if it's a screenshot. So for a screenshot, it has to be a image and then also screenshot has to be in name dot lower. And then I change into my desktop and then I iterate over os dot lister. And then I call the different helper functions. And then I cop or here I move the files into the corresponding folders that I want to use. For example, for an audio file, it moves the file into this directory. And then the same for videos, images, screenshots, and all other documents. And this is basically all that I use in order to keep my desktop clean. And then in order to automate this, you can run this script every once in a while as a cron job. And I have a full tutorial on my channel that explains how to run a Python script as a cron job. So check that out if you're interested. And this is all I wanted to show you in this tutorial. I hope you enjoyed this and then I hope to see you in the next video. Bye
Generate a brief summary highlighting the main points of the transcript.
GenerateGenerate a concise and relevant title for the transcript based on the main themes and content discussed.
GenerateIdentify and highlight the key words or phrases most relevant to the content of the transcript.
GenerateAnalyze the emotional tone of the transcript to determine whether the sentiment is positive, negative, or neutral.
GenerateCreate interactive quizzes based on the content of the transcript to test comprehension or engage users.
GenerateWe’re Ready to Help
Call or Book a Meeting Now