Bulk File Renaming Tool Project in Python
Key Takeaways
The video demonstrates how to build a bulk file renaming tool in Python using the os module and PyQt5 for the graphical user interface. It covers file renaming, regular expression filtering, and GUI development.
Full Transcript
[Music] what is going on guys welcome back in today's video we're going to talk about bulk file renaming in python what this means is that we're going to use python to take multiple files and automatically rename them according to our patterns or our renaming guidelines and in today's video we're going to do this in two parts in the first part i'm going to show you how to do that in a very basic way just using python without any graphical user interface without anything fancy and in the second part we're going to build a full gui application so a graphical user interface application where we can open up directories all the files are going to be listed we can select individual files then we have a collection of selected files and then we can apply our logic onto these files and they're going to be renamed according again to our patterns or guidelines so this is what we're going to build today let's get right into it all right so let's get right into it we're going to cover a couple of use cases here of course not all the possible use cases that we might want to cover but this is just a general introduction to the whole thing of course you can come up with your own crazy algorithms the first thing we want to do is let's say we have a directory with a couple of files for example myfile.txt or mydocument.x and then hello.xls so an excel file for example whatever a couple of files without any particular pattern and we want to take all of these files from a directory and rename them with one new name and a counter in the end so for example we want to take all these files and just rename them to renamed1.txt renamed2 2.x so we keep the file endings renamed three dot xls and so on this is the first use case how do we do that for this what we need first is we need to import os because os is going to provide us with the rename function the rename function is essentially the same as the move function so it's essentially just taking one file and moving it to another place basically renaming it and the function is going to be called file renaming or let's call them call it rename files and all we need to provide here is the directory because we're going to take all the files of a directory we can later on also specify file types and so on but for now we're going to take all the files and we're going to give them a new name that we're just going to call new name and essentially what we want to do is we want to now get an iterator for all the files in the directory how do we do that we say files equals os dot list dear list directory and we pass directory here as a path and then essentially we create a counter initialized with 0 and then we can iterate over the file so we can say 4 file in files and all we want to do here now is we want to take one path and turn it into another path so all we want to do here is we want to say os.rename and here we need to provide now what is it now and what is it going to be after the renaming the problem here is of course we want to keep the file type so we need to first extract the file type so if a file is called whatever.txt it should be renamed to new name a new name and counter but we want to key the txt we don't want to change the txt to something else and because of that we need to extract the file type first we're going to call this file type and this is basically just going to be the file split at the dot and we want to get the last um last element of this split because split basically if we have a string like my file dot is cool.txt with a couple of dots uh it's going to split this into a list of four words in this case the problem is we don't want to have the first dot or the second dot we want to have the last dot because after the last dot this is uh the ending that we want to use now this is quite primitive because if you have something like tar gz this is not going to work anymore but again this is just an introduction introduction lesson here so we're going to work with that and essentially we're going to then say os rename and we're going to rename the directory that we're currently in plus a slash plus file so the current file we're going to rename this to directory plus slash plus new name plus string of counter plus file type and of course a dot in between those so that's a very simple rename function we can also print what is happening so we can say print renaming file maybe with a space here two and then new name plus plus file type ah of course plus string counter as well and of course important we always want to increase the counter otherwise it's going to be quite stupid because we're always going to use the same uh counter and of course in python we don't have an increment so plus equals one is the way to go and this is actually the first bulk renaming function we can use so let's run an experiment i'm going to go to desktop i'm going to create here a new directory i'm going to call this tester and inside of it i'm going to create a couple of files so we're going to have a text document testme.txt then we're going to create another document for example let's go with an excel worksheet my data dot xlsx and then maybe a word document test then another word document maybe test2 and then one more i don't know rich text a dot rtf now what i need to specify is this path here so i'm going to copy it from the explorer and i'm going to now call this function on this path so i'm going to say rename of course we need to make this a string and we need to double escape so actually we need to escape the backslash because otherwise it's going to treat it as a backslash t for example for tap and of course the function is not renamed but rename files and the new name is going to be let's call it my new name there you go now if we did everything correctly we're going to now see that this is going to rename the files and in this case you can see renaming hey rtf to new name 0 rtf my data xlsx is now mine my new name 1 and so on so this actually worked we can also see that this is actually the case of course you can also change the counter to start at one if you don't like the zero here because outside of programming people start counting at one obviously or from one so this is the first little function we can already use for some simple bulk file renaming now we can actually go ahead and modify this function a little bit to have a more advanced function so we can filter by ending for example by just adding an additional parameter ending [Music] and then we're going to just say if file dot ends with ends with ending then we're going to do all this otherwise we're not going to do it and this is essentially now a different function so for example if i add here docx and i run this let's rename this to something new like that now if i run this again you can see that only two files were renamed because two files are ending with docx and have the file type docx now this is not not only for file endings we can also specify certain patterns in general so if i know that i have a directory full of docx files i can also say okay give me something that ends with a number point or dot dot x so i can also specify patterns in the name itself not only a data type for example now in this case it's not going to really not really going to trigger but for example if i don't have the number here but in the beginning for example 0 something new 1 something new and so on but i also have different docx files i can also specify something new.x and all the files that end with this string here are going to be replaced by something else here now last but not least before we move on to the graphical user interface part i would like to show you how to work with regular expressions so how to use regular expressions instead of something simple like ends with or starts with or contains we can specify very complicated expressions that filter for certain patterns and file names and only if a certain pattern is matched then we're going to rename the file now for those of you who don't know what regular expressions are they're quite complicated to write and read sometimes so don't be confused this video is not about regex so if you don't understand it don't worry just continue watching the video or skip that part uh but essentially regex or regular expressions are something that allows us to specify a certain format and if that format is matched then we can proceed uh with the with the renaming part so in order to do that in python we need to import re which is a core python module so nothing has to be installed here and we're going to change this function now by removing the ending and renaming it to pattern and instead of asking if the file ends with ending we're going to say if our e dot match if the pattern that we have here matches the file or the file matches the pattern the file name matches the pattern if that is the case we're going to rename it now we can keep that function down here but we're going to replace uh the actual pattern and the pattern in our case for example could be let me just see if i have the directory so here we have a couple of functions starting with my new name and a couple of functions uh starting with something new now we can filter for example for uh something that begins with the keyword my and maybe in order to show you that this actually works we're going to create a new file here and we're going to call it my different file so also starting with my but not with my new name and what we can do now is we can say okay give me a file where i have my in the beginning and then point star point star basically means any character as often as you want this is the regular expression for that so my is what it has to start with this is just two letters m and y and then point stands for any letter and the star stands for as many times as you want so zero or a thousand times doesn't really matter if the file starts with mi basically this is going to match and we're going to rename this to success to see that this actually worked we can run this now and you can see my different file txt was renamed to success uh zero and then the different my name my new name files were also uh renamed to success and of course we can also do that with file types now so we can say uh point whatever and then point txt now i'm not sure if we have to escape the point maybe with a backslash i'm not a regex pro myself let's see if that works okay in this case it worked but the problem is we renamed it again to success so let's rename it to different there you go now i'm not sure if this actually works properly so let's see if that would still do the thing okay so i'm not sure if this is the proper regular expression for that but for example we can also now try something else we can say um let's create another file here that doesn't have a number in it so auto file there's no number in it all the other files have a number so we can say for example one pattern that we can specify is uh whatever how many times you want but in the end we want to have um a number so zero to 9 specified like that and then we want to have um dot whatever essentially so we want to have a number in between basically and or in general this means that we want to have a number in the name and if that is the case we are going to call this number there you go so now you can see that all these files are renamed to number 0 1 2 3 4 5 but the one file that didn't have any numbers other files still of the file.txt again this is not about regex this video i just wanted to show you how you can do that simply if you understand regex uh there are multiple regexes out there many many regexes out there that are multiple lines long so it can get very complicated but still if you understand regex you can use that as well for bulk file renaming all right so for the second part of this video we're going to build a graphical user interface like this one we're going to be able to open up a directory to for example go to desktop and test your select folder then we're going to get a list of all the files in that directory we're going to be able to then filter for certain files for example i can type something like number and then point star a regular expression filter files and then it's going to select the files that match this pattern i can also remove them um i can also type other for example here other point star filter files i can also go ahead and say for example all the txt files so whatever dot txt like that then we get only the txt files um and once i have a list of selected files i can do something down below here so i can add a prefix i can remove a prefix i can add a suffix remove a suffix or choose a new name pattern so in this case for example what i can do is i can say add prefix and i can say text underscore and by pressing apply you can see that now i have the files text underscore number one text underscore number two and text underscore other file i can also remove that prefix i can select these files here manually put them in the right into the right list and i can then remove the prefix text now you can see it's gone again i can also go ahead and add a suffix so let's pick these three files here add a suffix and the suffix is going to be underscore test for example apply there you go and now i can also choose these four for example choose a new name pattern and call them new pattern and then apply this and you can see we have new pattern one two three four so this is what we're going to build here so in order to build this graphical user interface we're going to use pi qt5 and in particular for the design we're going to use the qt designer so what we need to do is we need to install the qt designer and you also need to install uh from the command line pip install pi qt5 if you haven't done so yet now for those of you who are not familiar with pi qt5 at all you've never worked with it and you're a bit confused i have a tutorial on that i have a full crash course on pi qt5 so i'm not going to explain all the basics here of course if something is a little bit more complicated and does not belong to the very basic content i'm going to mention briefly how it works but for the basic stuff like a menu like a push button like a line edit i'm not going to talk about all the details there so what you want to do is we want to create a new main window we want to start with an additional menu here file and we want to add this open directory option here you can also skip that part if you want to you can design your own graphical user interface it's actually yeah just the way i did it you can also change this so we're going to choose a line edit there you go for the filtering so for the regex you could say then we're going to add a push button for the actual filter so we're going to call this filter like that we can make this a bit smaller and then essentially what i chose to do is i use two list views so one list view here and another one here we can actually make this a bit larger there you go um and then i also had two buttons for the selection essentially with two or actually three greater signs with less than signs or symbols and then i had a couple of radio buttons do we have a radio button here there you go radio button one radio button two or actually let's first of all um set the font sizes so we're going to actually select all of these ui elements and we're going to change uh the font size here to something else we're going to increase it a little bit um i don't think that we need to have it bold though right no we don't need that okay so we have increased the font size uh for this we're going to also increase the font size a little bit and we're going to copy the button so that we have five buttons and of course we can definitely make this more beautiful but we're not gonna win a design contest here so add prefix remove prefix now this is actually a bit too large add suffix remove suffix new name like that and then finally we have the apply button actually i'm going to copy it from this button here apply there you go now does this look like that yeah okay a little bit different but doesn't really matter so this is our thing now one thing that you can do just for the sake of readability is you can rename uh the stuff because what you're going to have to do in python is you're going to have to address the individual buttons so this this filter button here is actually not called filter button it's called push button like that and this function or this button here is called push button underscore two and this is called push button underscore four and so on so we will change that real quick to filter button to [Music] uh select button [Music] to remove button [Music] to apply button then this is going to be not line edit but filter edit this is going to be the select or actually note the yeah we can actually call this list view and this is going to be the select view and this actually fine i guess let's call this action open and here we're going to have the individual radio button so at prefix radio remove prefix radio add suffix radio guess what remove suffix radio and new name radio there you go so then we can actually or before we save that actually we want to select these two list views here and we want to change their uh where is it selection mode to extended selection so that we can select multiple items we're going to save that now to our desktop and we're going to call this uh bulk gui dot ui and then we're going to take that file and we're going to drag it into pycharm going to stop this thing here and we're going to drag this into our working directory and now we can start with the coding all right so we start with a new python file from scratch but we're still going to import os we're going to also import re and now we're going to also import from pi qt5 dot qt gui we're going to import the q standard item model and the q standard item now those two things are important because in the list view we cannot just treat the list view itself like a list the list view is a ui element and it has to have an item model in the background that contains all the elements and the item all is the item all and the individual items are the q standard items so this is what we need that for we also need from pi qt5 dot qt widgets we're going to import everything don't use wild card imports if you're working on a real project for tutorials it's fine for demonstrational purposes just for showing you uh how things work it's fine but in a real project never use wildcard imports um and then from pi qt5 dot uh or actually only from pi qd5 we're going to import uic all right and we can now go ahead and create a class for the graphical user interface we're going to say class my gui and we're going to extend here from the q main window and in the init function we're going to call this super constructor so we'll see super my gui self underscore underscore init and then uic load ui and reload the ui at the path bulk gui dot ui and we pass self here so it's loaded into the self object and then we say self.show and the rest is going to be done afterwards so this is the basic definition for loading the ui and then we're going to now say app equals q application we're going to leave the arguments empty so an empty list then we're going to say window equals my gui and then app dot execute underscore so exec underscore if i now run this this is the ui that we just created in qt designer now it doesn't have any functionality yet so i can press whatever i want here um i can press apply i can filter it doesn't really matter i can try to open something doesn't work it's just a ui design itself for some reason all my windows minimized here but essentially we can now go ahead and work with this ui the important thing is of course that we need to remember these names so whenever i want to access something because i don't redefine these things in the code the ui file already has these things defined so i just have to access them using the self object um and the first thing i want to do is we want to start by defining some default values so for example the directory by default is going to be a dot for the current directory uh and we're going to also initialize the file model or the item model actually so the file list model let's call this list model this is going to be a q standard item model like that then we're going to also have a select model now the question is now should we keep the python convention of using underscores or should we go with the pi qd5 convention i'm going to go with a pi qt5 convention and this is going to also be a standard item model and then we say self dot um dot select or what was it called i forget that this was the select view so the self dot select view is going to be the we're going to set the model of it to the self dot select model like that and also as i already uh have done this in the hashtag generator for those of you who watched it um we also want to keep track of the selections using a list so selected is going to be an empty list we don't want to have this only as an item all we also want to have a list um and now basically everything is going to be in functions and the buttons are going to trigger the individual functions now what functions are going to have we're going to have the load directory function we're going to pass for all of those now we're going to have the rename files function which is going to actually trigger the final renaming we're going to have the choose selection function [Music] we're going to pass this one as well and we're going to have a remove selection function and we're going to have a filter list function now all these functions don't have parameters because the parameters are going to be part of the ui so we're going to get the values from the individual things and we can actually start by defining by by connecting the buttons and the options to the actual function so we're going to say self dot again what did i call this thing here i call it action open actually it was called action open wasn't it self action open triggered is going to be connected to self dot load directory and of course we don't call the function we pass the function as an entity then self filter button dot clicked dot connect self dot rename files let me just double check again if that's actually the name filter button there you go then self dot select okay one more time let me just see here we have this is the select button this is the remove button and this is the apply button okay so the select button clicked will result in calling the select or the choose selection function then we can actually copy that and replace this by remove button and this is going to be the remove selection function and then self dot apply button oh actually i'm stupid this is going to be a link to the filter filter list function here and this one is going to do the renaming like that there you go so that is how we're going to do that we can also see if that works by removing for example uh the pass and printing some test messages here so let's see if that works if i now press this here we see open in the command line if i press this here we see test okay so the buttons are linked to the functions at least some of them um [Music] and we can now replace this with pass again uh but this is actually all we need to do in terms of linking the buttons to the actual functions now everything else is going to happen inside of the functions and the first function is actually quite simple it's just going to load the directory it's going to take uh all the files from that directory we're going to iterate over them create q standard items out of these file names and then we're going to add them to the list so we're going to say self.directory is going to be os dot not no it's not going to be os dot anything it's going to be q file dialog q file not file name okay the auto completion is a little bit annoying uh cue file dialog dot get existing so the directory already has to exist and this is going to be select directory as a title and the result of that is going to be the directory and then we're going to say for file in os dot list there so basically what we did before self.directory for all the files here we're going to say okay if os dot path dot is file and then file or actually os path join and we need to join here the self.directory and the file there you go only then are we going to add this to the file model so self dot i call it different name here i call the list model so self dot list model dot append row and we're going to append a q standard item of file now why are we doing all of that we're going through all the files in the directory or through all the things in the directory and some of them are going to maybe be other directories now of course you can take the time and do this recursively you can also go deeper into other directories and list their files if you want to but in this case we're only interested in the file so we don't want to know other directories in that directory we only want to get the files and because of that we check if at this particular file path we find a file instead of a directory and then we add it to the to the list model and in the end what we're going to do is we're going to say self dot what was the list view i think it was just list view right set model and we're going to set the self file model uh now i'm not sure if we cannot already do this up there but for now and why do i always call this file model it's called file model in my prepared code so i always type file model it's actually called list model obviously um and this here is a duplicate code fragment only because i have this code already prepared so again what we do is we open up a file dialog we select the directory we go through all the files all the files that are actually files and not directories we append them to the list model and then we set the list model to list view i think this should actually already work so let's give it a try let's open a directory let's go to desktop test directory select folder uh no it crashes why does it crash though i'm not sure we're going to see let's give it a try accept exception s e and we're going to print e with pi q t 5 you need to do it like that otherwise you're not going to get the error message so let's open the directory again let's go to the tester let's select a folder my gui object has no attribute list model okay because we need to have a capital m here i think right at least i think so let's run this again open directory tester select folder there you go now we have it so this works now let's move on to the next function which is choose selection and remove selection those are actually quite simple we're going to the renaming in the end so the choose selection function is going to just say if the length of the selected file so on the list view dot selected indexes so if we have selected indexes and so basically if that is not zero if the amount of selected indexes is not zero then we're going to choose them then we're going to move them to the uh selected view so we're going to say four index in self.listview.selectedindexes we're going to say if the index data is not already in self-selected because we don't want to add files multiple times if it's not already part of selected we're going to say self dot selected dot append index data like that and then self dot uh select model dot append row q standard item index data and the thing doesn't detach so let me just kill it here um this should actually work so let us also implement the remove selection function here basically we're saying try i'm going to try to or actually do i need this or did i only do that because i wanted to troubleshoot we're going to try it with a try except but essentially we're going to say if the length again of the self dot this time the other one so off the uh select view or what did i call it select view yes if the length of the select view dot selected indexes is equal it's not equal to zero we're going to reverse the order i explained this in a video in the past already because if we don't reverse the order we're going to um to remove elements and then we're going to try to remove elements at the same position again or at different positions so i'm going to show you in a second uh why we're going to do that but we're going to see for index in reversed sorted self.filtered actually not filtered select select view dot selected indexes like that for each index in there we're going to say self.selected dot remove the element from index data and then come on then self dot select model dot remove row index row uh what's the problem here now i don't know what the problem here is oh of course we need an accept exception as e in case something goes wrong we're going to print that uh but besides that actually this is the function so again let me show you why we do that i hope it already works and i can show to you why we need to do that the problem is that if i open some files and if i move them to the right here if i select now a couple of files here they're going to to they're going to be removed one by one and because of that we're going to they're going to change positions so essentially if i remove these files now it's going to work because i reversed the order but i'm going to show you what happens if i don't do that so let's just delete reversed and sorted it's not always going to produce problems but oftentimes it's going to produce problems and we don't need these problems so let's open that up if i now add a couple of them and i now go ahead and remove a couple of them there you go not in list because the order and and the whole program is going to crash or uh in this case it's not gonna it's not gonna crash because i accept uh i catch the exception here and i print it but the problem is that if i don't reverse the order it's not going to go from back to the front so it's going to shift positions and then it's going to try to remove something that isn't there because of that we want to remove we want to reverse and sort the order that way it works um besides that we're now going to go to the filter list and then last but not least we're going to implement the renaming itself so for the filter we're going to say self dot select model dot clear essentially saying that everything that is selected it's going is going to be cleared so we don't have it anymore um so that we can actually only have the matches of the regular expressions in there and then we're going to say for index in range and then self dot list model dot row count for all the uh elements that we have there we're going to say item equals self dot dot list model dot item index so we're getting the individual items from the individual indices here so we're getting all uh all the individual indices and we're getting the items of these indices and then we say okay if the item that i have there if the file name that i have there matches the regular expression so if re match and the pattern is of course um part of the what did i call it filter edit in this case self dot filter edit dot text there we have the pattern and the item itself has a text as well like that and if the pattern matches then we're going to say self dot select model dot append row q standard item from item text and of course self.selected.append item dot text by the way i don't have that in the prepared code but i think we should also not sure if i'm if i'm forgetting something right now but i'm pretty sure we also should set selected to an empty list otherwise we're going to have some problems all right so that's filtering removing choosing and loading now we're going to rename the files and for that we need to focus on the radio buttons because the radio buttons determine how we rename the files how do we rename them we have the add prefix we have the remove prefix we have the suffix adding and removing and then we have the new name so essentially we're going to start by defining a counter as one and we're going to say if self dot now we need to see how do i call them again i called them at prefix radio remove prefix radio and so on and then you name radio so okay if add prefix radio dot is checked and only one button can be checked at a time so we don't need to check if all the others are not checked if it is checked we're going to say os rename and then os path join the self directory and the file name now the file name uh oh sorry i forgot to open up the loop we need to say of course for file name in self.selected because obviously uh the renaming is only going to be applied onto the selection so we don't need to look into the views actually we only need to look into the list that we have the selected list and for each file name we're going to see okay is uh we're going to rename it according to the selected option so in this case we're going to rename the file at this position to os path this is a bit tedious because uh the lines are getting too long here os path join and then self dot directory and then self dot rename we're adding a prefix right so self dot what did i call this thing there oh i forgot a major element i forgot a major element in my graphical user interface this is not good we need to add that right now we need to add the line edit that is responsible for choosing the actual name the actual suffix prefix or new name that we add to the file name so uh what font size did we choose here we chose 14 here we're going to choose 14 as well uh let's call this name edit save this onto the desktop again and now we need to replace that here overwrite there you go so what we want to add here is we want to add to the directory um so tutor to the path to the directory we want to add first of all the prefix so the name name edit dot text plus the file name that we already have right now and i think that is actually it for that option now let me just check if i'm not blocking anything here no i'm not and then we have the other cases as well so we have the case that self dot remove prefix radio is checked and in that case we need to check first of all if the file name actually starts with the prefix because otherwise it doesn't make sense to remove it so we're going to say if file name dot starts with starts with self dot name edit text if that is the case then what we're going to do is we're going to rename the current file name again like that we're going to rename this here to and we're going to remove by using index slicing we're going to remove um i'm going to say again os path join and here we're going to say file name but from the position length of self dot name edit dot text so however long that prefix is we're going to go to that position and we're going to take this string from there so this should actually be it then we have alif self dot at suffix radio is checked if that is the case we're going to say os rename um and essentially we can copy the prefix stuff here but we need to or actually we cannot copy it because we need the file type we need to consider the file type so we need to say actually file type equals file name dot split on a point and we want to get the last result again so this is the file type and then we're going to say okay we rename the current file to and we're going to add a suffix so we take the current file plus whatever is part of the name edit so name edit text like that plus and then a dot and then the actual oh now i messed up a couple of things sorry what we need to do is let's remove all this first so we need to add self dot or actually the file name first the file name first then the suffix so self dot name edit dot text then dot then file type that's it all right so we have that now we also have the option of removing a suffix remove suffix radio is checked and here essentially we need to do the same thing we need to uh consider the file type so the file type actually let's copy this here the file type is what the file type is and then we're going to say in this case if the file name ends with um with self name edit text and also the file type or dot file type then we're going to do that what are we going to do we're going to say os rename os path current to os path join self.directory and we want to remove the we want to remove the suffix so we need to work with slicing again what we're going to do essentially is we're going to take the file name up until the point were the thing begin so minus the length of self dot name edit text plus a dot and plus the file type so we're going to take the first part of that and then plus a dot and a file type there you go and then last option is elif what if we want a new name this is essentially what we did all the time so self dot new name radio is checked if that is the case what we're going to do is we're going to take the file type again and then we're going to essentially say rename again os path join file name to os path join directory file name uh actually not file name but what we want to have is we want to have the new name so the self dot name edit dot text plus a point or a dot and then the file type and of course don't forget the counter so plus str counter and of course the counter needs to be increased by one there you go and we can also say else otherwise if we don't select anything we're going to say select a radio button and once all of this is done so basically when we have renamed the files what we're going to do is we're going to say self dot selected equals an empty list self dot select model is going to be cleared self dot list model is going to be cleared and for file in os lister of the current directory we're going to say again if um the file is actually a file so we're going to basically update the list if the file in that iteration is a file then self dot file model dot list model we're going to append to it the row the q standard item model or the q standard item of the file and in the end self dot list view is going to get the model of self dot list model there you go so this part was a bit tedious and it's prone to errors so let's see if it actually works um did i start a debugger now come on let's run this and see if it works on our test directory so open the directory go to desktop go to test directory select it choose a couple of those then add a prefix test underscore apply this works so now let's filter here for something containing uh the letter or nu in the text so we can say whatever and you whatever filter and here we get these three here so we can remove um or actually not remove let's add a suffix new underscore new apply this works uh now actually doesn't work so it added it added the suffix but it also added the file type so we need to fix that real quick so the add suffix has to consider that we don't take the file name and then add the file name uh but we take the file name up until the actual um up until the the file type itself so up until the length of the file type now it was plus or minus one let me check it was plus one but it was in parentheses because otherwise it's going to go the other way all right so this should work now open it again test directory select folder filter again for new and then there you go and now let's first of all give them a new name test me apply there you go now we can take all these put them on the left on the right here and we can add a suffix here with an underscore before that and now we have a problem why do we have a problem let's see what the problem is we're going to have a try and an accept we're going to print the exception and see why it doesn't work okay so it did work for some of them let's give them a new name hello there you go and now choose them underscore new and then add suffix apply cannot create a file that already exists it tried to create oh okay when a file already exists of course we need to in this case what did it do it actually took hello3.txt and it tried to rename it to hell new dot txt so this is a problem with the add suffix so the add suffix has a problem we just double check here we have up until oh oh oh negative that we want to go from right to left i messed up the order so let's run this again now it should work pretty sure that it will work now so let's take those again let's give them a new name apply let's take them to the right add a suffix now it works and then apply there you go now it works actually and we can also try to remove it so let's put it to the right let's remove the suffix uh here i have a typo direct audrey so we need to go with directory directory where do i have this directory and one more time reboot but after that it should work but this is the real programming guys whenever you write code you're going to find some stupid mistakes and then you're going to have to fix them so again let's remove the suffix underscore now it works there you go okay so this is how you build a bulk file renaming tool in python so that's it for today's video hope you enjoyed i hope you learned something if so let me know by hitting the like button and leave a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Original Description
Today we learn how to build a simple bulk file renaming tool project in Python.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
🎵 Outro Music From: https://www.bensound.com/
Timestamps:
(0:00) Intro
(0:57) CLI Project
(13:42) GUI Project
(57:19) Outro
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
Your AI Knows Everything About React. It Knows Nothing About Your React Project.
Dev.to AI
I Built a Free AI Architect to Stop Spaghetti Code in GitHub Actions
Dev.to AI
Alibaba Bans Employees From Using Claude Code Following Security Concerns
Medium · AI
Cursor Pricing 2026: Free vs Pro vs Ultra — Which Plan?
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI