File Organizing with Python: Rename, Move, Copy & Delete Files and Folders

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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.pi 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 dir 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 lister 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 split 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 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 splitted of 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 2 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 renamed 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 worked as well with the new syntax using the pathflip 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 maker 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 or 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 specified 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 is last thing let's learn how we can delete a file and folder again so for this we can use 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 rm3 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.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 copy 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
Info
Channel: Patrick Loeber
Views: 46,739
Rating: undefined out of 5
Keywords: Python
Id: NOvFZamGXXo
Channel Id: undefined
Length: 12min 2sec (722 seconds)
Published: Sun Jun 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.