how to get a full list from a function python
Question: I'm totally new about python. So here is my issue.

    
    
    def visitdir(path):
        result = []
        for root,dirs,files in os.walk(path):
            for filepath in files:
                result = ''.join(os.path.join(root,filepath))
                print result            
    if __name__ == '__main__':
        path = raw_input("Please input the folder you want to walk:")
        visitdir(path)
    

It will give the full list while inputting the folder I want to walk. But if I
modified it as below:

    
    
    import os, sys
    def visitdir(path):
        result = []
        for root,dirs,files in os.walk(path):
            for filepath in files:
                result = ''.join(os.path.join(root,filepath))
                return result            
    if __name__ == '__main__':
        path = raw_input("Please input the folder you want to walk:")
        final = visitdir(path)
        print final
    

It will only give one item from selected path. If I just want to the same
result as previous one, How do i invoke the function?

Thanks in advance.


Answer: When you call `return` you finish the function flow and return the value. Also
why do you create the list 'result' and then override it with the string?

I think what you meant to do is something like this:

    
    
    def visitdir(path):
        result = []
        for root,dirs,files in os.walk(path):
            for filepath in files:
                filename = ''.join(os.path.join(root,filepath))
                result.append(filename)
        return result
    

eg filling up the list with results and returning it in the end.

Another thing you could do is to use
[yield](https://pythontips.com/2013/09/29/the-python-yield-keyword-
explained/).

