Filenames and paths trouble
The following example "walks" through a directory, prints the names of all the files, and calls itself recursively on all the directories. 

        import os
    def walk(dir):
        for name in os.listdir(dir):
            
            path = os.path.join(dir,name)
    
            if os.path.isfile(path):
                print path
            else: walk(path)

    os.path.join` takes a directory and a file name and joins them into a complete path.

My exercise: Modify *walk* so that instead of printing the names of the files, it returns a list of names.  

Can someone explain what this function is doing per line? I have a good idea but when it gets to the line: `else: walk(path)`, that throws me off since there is no explanation what that is doing. For this exercise, the only way I could think to change this to a list is by: 

   

    def walk1(dir):
    res = []
    for name in os.listdir(dir):
        
        path = os.path.join(dir,name)

        if os.path.isfile(path):
            res.append(path)
            
            
        else:
            walk(path)

    return res

My output still builds like the first example and at the end, returns a list but of something else. I'm more concerned with the explanation than the answer. 


Filenames and paths trouble
===

The following example "walks" through a directory, prints the names of all the files, and calls itself recursively on all the directories. 

        import os
    def walk(dir):
        for name in os.listdir(dir):
            
            path = os.path.join(dir,name)
    
            if os.path.isfile(path):
                print path
            else: walk(path)

    os.path.join` takes a directory and a file name and joins them into a complete path.

My exercise: Modify *walk* so that instead of printing the names of the files, it returns a list of names.  

Can someone explain what this function is doing per line? I have a good idea but when it gets to the line: `else: walk(path)`, that throws me off since there is no explanation what that is doing. For this exercise, the only way I could think to change this to a list is by: 

   

    def walk1(dir):
    res = []
    for name in os.listdir(dir):
        
        path = os.path.join(dir,name)

        if os.path.isfile(path):
            res.append(path)
            
            
        else:
            walk(path)

    return res

My output still builds like the first example and at the end, returns a list but of something else. I'm more concerned with the explanation than the answer. 

