How To Use Recursion In Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone thank you for joining again my name is Taylor and today I'm going to teach you about recursion in Python recursion allows us to call a function within a function I've already created a python file and now let's go ahead and import the OS module so we'll say import OS the reason why we want to import the OS is to list directories and show contents within those directories so now we have have this let's go ahead and create our main function and after we create our main function we're going to say print os. get CWD what this is saying is from the OS module use the get CWD command to get the full current working directory of our file here so let's go ahead and save this and run this program by typing python in the name of this file as you can see here is my full directory path now let's say we wanted to see the files within this directory we can do that using the os. list command so we'll say print os. list dur open and close parentheses so if we run this program again you're going to to see that the file is this python file which is this instance here what if we wanted to go into the PC here and list the directories we can do that if we go into the list D put dot dot slash and save and as you can see it shows everything within this directory but say we wanted to do this recursively instead of having to constantly do the dot do slash do slash we can actually do that programmatically using recursion let me show you how to do that now so let's clear the screen here and then let's go ahead and comment this out and comment this out and now let's create a function here that will be our template for our recursion we'll say DF space wreck open and close parentheses add a colon at the end so we want to use the os. getet CWD command in order to get the current working directories and it's subfolders when I say subfolders I mean anything before the current working directory so for instance the desktop and before that is the PC and before that was home and before that is root I am on Linux so your file structure may be different if you're using Windows you may see C SL users it depends on what operating system you're using so let's go ahead and set the variable name for os. getet CWD by saying full directory is equal to os. get CWD open and close parentheses now let's go ahead and send this to our re function here the way we do that is by calling Rec open and close parentheses in those parentheses send in full directory let's save this now let's go ahead and create a argument within the recc function so we can say FD for full directory and now let's go ahead and hit enter okay now we need to establish our conditional statements we'll say if open and close parentheses FD double equals open and close parentheses blank why am I doing this well this is saying if the current directory is empty then do something so let's go ahead and add a colon at the end and say print recursion stopped and then return okay now let's add our else statement so else add a colon at the end so what do we want to do now we want to recursively go backwards until we've reached the end point or that this FD is blank we know that the directory has a forward slash we need to remove the name after the forward slash but we only want to remove the last item or the last name after the forward slash the reason why we want to do that is to send it back to itself but with a new name we need to format the directory and we can use the RIT function in order to do that let's go ahead and do that now so I'll say format directory is equal to and we know that FD represents our current working directory and then we need to sayt R spit open and close parentheses then we need to send in what we are going to split on we're going to split on the forward slash add a comma and then we have to specify which item we are going to split on and we want to specify one because that is the last item within the string FD now let's go ahead and hit enter and recall the rec and then we'll send in format directory at index0 why do we use at index0 as you recall we used RIT and this RIT created a new array it has two items in it it has the full directory and the single directory for example if we had home and then PC SL desktop here this R split is splitting at this last forward slash so all that is remaining at index0 is this slome slpc now let's go ahead and add a print statement before the splitting so we'll say print and then we will say FD let's save this and let's run this program by typing python re. py as you can see it went to the full directory here then it took off the for SL desktop that's what this R spit is doing and then it returned the index zero here and then it took off the forward slpc and then it returned just the forward slome and then finally the recursion stopped because FD was equal to blank now if you wanted to list all the files within each directory it's as simple as calling os. list dur so let's go ahead and say print os. list dur open and close parentheses and send in FD we use FD because it is changing every time we call re in the function itself let's go ahead and clear the screen and rerun the program as you can see it shows everything within each directory I hope this has helped you understand how to use recursion and how powerful it is when traversing through directories thank you all for watching I invite you to subscribe and I hope to see you next time
Info
Channel: Taylor's Software Solutions
Views: 3,478
Rating: undefined out of 5
Keywords:
Id: JLYvUxgbq28
Channel Id: undefined
Length: 8min 50sec (530 seconds)
Published: Fri Mar 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.