Tuesday 25 June 2013

SPSE update - Recursive function

Hi all, just a post to myself and to the public to update on how I have been tracking.

Also to grab onto the many bits of pieces of information bouncing around my brain. Tracking them down so I do not lose i all!

SPSE Recursive Function

So Module 2 Video 1 asks you to complete an exercise. This exercise is to create a python file that recursive searches a directory and prints them out in a hierarchical format.



The following is the code I ended up with

Linking code in a blog can be found here https://gist.github.com/
There is a way through javascript however to directly embed it, I do not have time so this is it for now. It worked but was not able to find the python version.

Anyhow..

First off I had no idea what a recursive function was, so I was able to simply use a loop to go through os.listdir() of all the directories.

A recusive function as I understand from friends telling me about recusrive factorial funtions, is that it is one that hands itself back to itself. Great english hey.

By that I mean at the end of the function (loop, if, whatever) it passes the value back into the value being called in function. So if a function used 5 as its value and took 2 away it would then return the value 3 to the function and it would minus 2 again.

This could happen forever but the function does need a limit/stopping point (termination condition) as well as a reduction factor to allow it to reach that stopping point.
Here is some good suggestions http://stackoverflow.com/questions/479343/how-can-i-build-a-recursive-function-in-python


The following is code that has no termination condition. It would simply loop with the words hello world incrementing the number



Back to the recursive function at the top.

My biggest problem was trying to get os.listdir or os.walk to continue printing the items out without looping back over the directories.

So I wanted  code that would list the directories and paths together, so I needed something to store what directories and files had been looked over so that it could be recursive and send that information back to itself. This was to be stored in the newdir array/list. As then it could choose that as the new directory and continue from there.

It would have been START directory do loop find A directory with A file in it. Then it starts over and start directory is now A directory. if no more directories this is not passed to the start of the function again. Making it clear the termination factor (became clear now I have written it).



The above is to do with os.walk . This is the easy for loop for printing out a directory and files. This walks through all the directories and sub directories. My trouble is with the dirtrav code I am unsure how it walks subdirectories. I have tried for loops with os.listdir but was not able to go into subdirectories. This really frustrated me.

I broke the dirtrav code apart and understand how and why and what it was doing, but still unable to see why. If you delete the for f in newdir loop it does not print out the files within subdirectories, so it must do something there.

**NB get javascript embed code to work, so annoying creating it externally, going into html edit and pasting then coming back out.

Simply I Googled code to see if there was any starting points to give me an idea and it came up with the top code, so I spent much time working out different ways to make it recursive. But using the array/list gives a very simple termination factor without using integers, says num -1 for example. As it is to do with directories and there can be a different amount of them the stopping based on the intergers would never be suitable.

Code output when dirtrav(f) is not deleted



When dirtrav(f) is deleted, not sent back to itself.





So you can clearly I have not fully understood the code.

EDIT:!! So as it passes f back into dirtrav. it doesnt print the directory because there is none. it prints the file names... *facepalm*



Hopefully this helped some people out.
Cheers
Haydn




No comments:

Post a Comment