0

What would be a good way to get a recursive function to build a list for return? Let's say I have a function like the following for generating a listing of files and directories at all subdirectories:

def print_directory_listing(directory = "."):                          
    for file_structure in os.listdir(directory):                
        file_structure_path = os.path.join(directory, file_structure)
        if os.path.isdir(file_structure_path):
            print_directory_listing(file_structure_path)
        else:
            print(file_structure_path)

Rather than printing out everything, how could I modify this to return a list of all of the files and directories? For example, would it be good to have a global list to which the function appends files?

Note that I am not asking for details on os.walk; I am asking a general question on what the Pythonic way would be to get a recursive function to build and return a list.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
d3pd
  • 7,935
  • 24
  • 76
  • 127
  • 1
    Why you don't use `os.walk()` which is exactly for this aim? – Mazdak Nov 22 '15 at 14:54
  • 1
    @Kasramvd Why don't you make an answer instead of a comment, since this is definitely an answer ? – Loïc Nov 22 '15 at 14:55
  • @Loïc Because this question is duplicate. – Mazdak Nov 22 '15 at 15:01
  • 1
    @Kasramvd My question is *not* about creating a directory listing; it is about how to have get a recursive function to build a list and return that list. The fact that I mentioned directory listings is entirely incidental. – d3pd Nov 22 '15 at 15:03
  • 1
    This is not a good example for demonstrating your task, you better to ask a question with providing a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). As a hint you can also see the source of `os.walk()` function which uses a recursive approach https://github.com/python/cpython/blob/master/Lib/os.py#L298 – Mazdak Nov 22 '15 at 15:12
  • @Kasramvd How is this not a good minimal example? The full function is given, using simple code, doing a common task. Thanks for the link to the `os.walk` function; I'm trying to understand it now. Could you turn off the "duplicate" status for this question? The "duplicate" you have suggested does not address the question. – d3pd Nov 22 '15 at 16:07

1 Answers1

1

Thanks to your comment I think having understood the real purpose of your question. If you take a look to the following code, you will find the recursion you are looking for and you will see how to "save the variables of the childs".

import os

def print_directory_listing(directory = '.'):
    files_list=[]

    for file_structure in os.listdir(directory):                
        file_structure_path = os.path.join(directory, file_structure)
        if os.path.isdir(file_structure_path):
            files_list+=print_directory_listing(file_structure_path)
        else:
            files_list.append(file_structure_path)

    return files_list
  • Thank you for your solution. My question is not specifically about creating a directory listing; it is about how to have get a recursive function to build a list and return that list. – d3pd Nov 22 '15 at 15:06
  • I have an example code for you, I cannot add an answer to the question. It is closed. Will you ask a new question? – Ozan Nov 22 '15 at 15:31
  • @d3pd I just Updated my answer –  Nov 22 '15 at 17:27