question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can you create folders using a path ?

See original GitHub issue

Currently you have a method called “create_child_folder”, is it possible to extend this method to support “path” (https://docs.microsoft.com/en-us/graph/api/driveitem-post-children?view=graph-rest-1.0)

OR

create a new method that supports path ie “create_folder_by _path” ? or “create_path_folder” ?

looking at the method, you have a data dictionary where you are building up the json response, so it should be a matter of adding (taken from docs.microsoft.com) to this dictionary:

  "parentReference": {
    "driveId": "5FE38E3C-051C-4D55-9B83-8A437658275B",
    "id": "E67A8F34-B0AA-46E1-8FF7-0750A29553DF",
    "path": "/drive/root:/"

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
slash5k1commented, Apr 14, 2019

heres my example of “create_folder_by_path” function - will park this code here and hope it helps someone in the future 😃

class O365Account():
    def __init__(self, client_id=client_id,
                 client_secret=client_secret, scopes=scopes):
        self.client_id = client_id
        self.client_secret = client_secret
        self.account = Account(credentials=(client_id, client_secret))
        self.account.is_authenticated ## have a saved token to work with

        self.sharepoint = self.account.sharepoint()
        self.site = self.sharepoint.get_site('xxxxxxxxxx.sharepoint.com','/sites/PlayPen')

account = O365Account()
playpen = account.site

def return_document_root(document_library):
    for library in playpen.list_document_libraries():
        if library.name == document_library:
            return library.get_root_folder()

def check_if_child_folder_exists(parent_folder_object, new_child_folder):
    for item in parent_folder_object.get_items():
        if isinstance(item, Folder):
            if item.name == new_child_folder:
                return item

    return None

def create_folder_by_path(document_library, path):
    root = return_document_root(document_library)

    for count, new_folder in enumerate(path.split(os.sep)):
        if len(new_folder) == 0:
            continue

        ## create top level folder from root
        if count == 1:
            returned_folder_object = check_if_child_folder_exists(root, new_folder)

            if returned_folder_object is None:
                parent_folder_object = root.create_child_folder(new_folder)
                print (new_folder, " - Folder Created")

            else:
                parent_folder_object = returned_folder_object
                print (new_folder, " - Folder Already Exists")

            continue

        ## create folder, next level down from parent folder
        if "{" in new_folder: ## support subfolders at the same level with the use of {}
            list_of_folders = new_folder[1:-1].split(",")
            
            for new_folder in list_of_folders:
                same_level_parent = parent_folder_object
                returned_folder_object = check_if_child_folder_exists(same_level_parent, new_folder)

                if returned_folder_object is None:
                    same_level_parent.create_child_folder(new_folder)
                    print (new_folder, " - Folder Created")

                else:
                    print (new_folder, " - Folder Already Exists")
            break ## break the loop after we have created a list of subfolders at the same level

        else:
            returned_folder_object = check_if_child_folder_exists(parent_folder_object, new_folder)

            if returned_folder_object is None:
                parent_folder_object = parent_folder_object.create_child_folder(new_folder)
                print (new_folder, " - Folder Created")

            else:
                parent_folder_object = returned_folder_object
                print (new_folder, " - Folder Already Exists")


path = "/test folder/blah/apple/Once Upon @ time/{dir1,dir2,dir3}"
document_library = "Documents"
create_folder_by_path(document_library, path)
0reactions
janscascommented, Apr 15, 2019

You will have to support this by yourself, as you did already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Create a Directory or Folder - Computer Hope
Create a folder in a directory · Open Finder and navigate to the directory where you want to create the folder. · Click...
Read more >
How can I create a directory if it does not exist using Python?
In Python, use the os.path.exists() method to see if a directory already exists, and then use the os.makedirs() method to create it.
Read more >
How can I safely create a nested directory? - Stack Overflow
On Python ≥ 3.5, use pathlib.Path.mkdir : from pathlib import Path Path("/my/directory").mkdir(parents=True, exist_ok=True).
Read more >
Create a new folder - Microsoft Support
Open File Explorer using one of the following methods: · Navigate to where you want to create the new folder, and click New...
Read more >
Creating file paths - FileMaker Pro
Specify one or more directories in a path. file:$username/$fileName. file:JohnSmith/test.xlsx ; Specify absolute paths or relative paths, with various path type ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found