Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 8: File Searcher App
Lecture: Recursion applied
0:01 Ok, let's see how recursion will us search not just the current directory that we give it
0:06 but the entire directory tree starting at the directory we provided as the root.
0:12 Here we have search folders, this is going to be our recursive function, and we are going to come here, we are going to list all the items here,
0:18 we want to for each item build up the full path to that item. Remember these little bits are just relative,
0:24 they are sort of the name but not including the path. Before we said we don't know what to do with directories
0:29 let's just get out of here and then we said if it is a file we will go get all the matching lines and information about that in extend to all matches,
0:37 well now we can use recursion to solve this problem so let's try this like else really quick, so we sort of put these in these two cases
0:45 so if it's a directory we do one thing otherwise, if it's a file we go search it. so what are we going to do,
0:51 we are just going to say if it's a directory, let's also search that directory and what method do we have that can search folders,
0:59 well, the one that we are in. But we are going to search a different folder, a subfolder, and the name of the subfolder is full item
1:05 and we still want to search for the same text, now this will come back with matches as well,
1:09 different sets of matches probably more, you never know, right, but it's all the stuff within that subfolder not just an individual file,
1:16 and then we'll say all matches.extent those matches as well. Look how perfectly recursion solves this problem
1:23 like it was really one or two lines of code, we removed the continue we just said search but a smaller subset of this tree
1:33 we want to go search and we are just going to recursively do that until there is no more directories we are just down to the end,
1:38 the end of that hierarchy if you will. So let's run this and see if it's working. So again, we are going to search our books,
1:44 now remember there is that classics folder that has Sherlock Holmes in it previously we did not have results for Sherlock Holmes
1:50 so hopefully we are going to search the books folder and within there we are also going to search the classics subfolder, let's give it a shot.
1:57 Again, we are going to search for Holmes, and here we go, wow, now we have a lot more results and we've got the Ulysses book
2:04 but we also have classics, The Adventures of Sherlock Holmes you can see on line 11587 the table had not been cleared yet, Sherlock Holmes had been...
2:14 beautiful, and you saw that recursion was the key to making this work in the most dramatically simple way possible. So in some sense our app is done,
2:25 if what we want to give our app is 10 books that are just all text and maybe that's a couple of megabytes
2:33 you can see that we can actually search that quite quickly and there is no problem,
2:37 but if the number of files and the quantity of files gets to be gigabytes of text
2:42 you'll see there is a severe performance problem especially around memory and we can do much better using some very cool features
2:50 called generator methods that's what we are going to do next.