jeudi 4 avril 2019

How to solve directory path problem recursively in Python?

I am doing REST API calls to fetch folders of a SharePoint document library.

I want to recursively get all the folder paths in the entire directory tree.

I have written a function to get a list of sub-folders from a given folder, but am not sure how to traverse till N-th directory and get all folder paths.

Suppose for example, the present SharePoint Document Library structure is as the following JSON (fo=folder; f=file):

{
  "root": [
    {
      "fo1": {
        "fo1": "f1",
        "fo2": ["f1", "f2"]
      },
      "fo2": ["fi1", "fi2"]
    },
    "fi1","fi2"]
}

From the above example, I want a list of paths of all folders/directories: Eg output should be:

["/root/fo1/", "/root/fo1/fo1/", "/root/fo1/fo2/", "/root/fo2/"]

Since it is a REST API call, so I do not know the structure from beforehand until I run a query of get subfolders and then going inside each subfolder to get their respective subfolder.

The present (following) function I wrote is getting me the data till 1 level down (subfolder, since it is inner iteration based and not recursion), how can I achieve a recursion based solution to get all the unique folder paths as a list?

def print_root_contents(ctx):

    try:
        list_object = ctx.web.lists.get_by_title('Documents')
        folder = list_object.root_folder
        ctx.load(folder)
        ctx.execute_query()

        folders = folder.folders
        ctx.load(folders)
        ctx.execute_query()

        for myfolder in folders:
            print("For Folder : {0}".format(myfolder.properties["Name"]))
            folder_list, files_list = print_folder_contents(ctx, myfolder.properties["Name"])
            print("Sub folders - ", folder_list)
            print("Files - ", files_list)

    except Exception as e:
        print('Problem printing out library contents: ', e)


def print_folder_contents(ctx, folder_name):

    try:
        folder = ctx.web.get_folder_by_server_relative_url("/sites/abc/Shared Documents/"+folder_name+"/")
        ctx.load(folder)
        ctx.execute_query()

        # Folders
        fold_names = []
        sub_folders = folder.folders
        ctx.load(sub_folders)
        ctx.execute_query()
        for s_folder in sub_folders:
            # folder_name = folder_name+"/"+s_folder.properties["Name"]
            # print("Folder name: {0}".format(folder.properties["Name"]))
            fold_names.append(s_folder.properties["Name"])

        return fold_names

    except Exception as e:
        print('Problem printing out library contents: ', e)

In the last function (print_folder_contents) above, I am unable to form a recursive logic to keep appending folders and subfolders recursively and stop it when there is no more folders inside the n-th folder and continue for next sibling folder one level up.

Finding it really challenging. Any help?

Aucun commentaire:

Enregistrer un commentaire