API: get list of files in the folder recursively
Is there an API call I can use to get list of all files and folders in a specific folder? I see there is a "search" call but it requires a "query" parameter and does not accept "*".
-
Hi ,
You'll want the List items in folder endpoint, that should give you the information that you're looking for.
- Jon
-
The code below works for me. Here's my folder/file structure:
- Folder1
- * file1.1
- * file1.2
- Folder2
- * file2.1
- * file2.2
- file1
- file2
import os
import boxsdk
auth = boxsdk.OAuth2(
client_id=config['boxAppSettings']['clientID'],
client_secret='',
access_token=access_token
)
client = boxsdk.Client(auth)
def get_recursive(dir_list):
for item in dir_list[-1].get_items():
file_info = item.get()
if file_info.type == "folder":
dir_list.append(item)
yield from get_recursive(dir_list)
else:
yield "/".join([x.get().name for x in dir_list]) + "/" + item.get().name
dir_list.pop()
print(os.linesep.join(list(get_recursive([client.folder('0')]))))Which for my structure prints:
All Files/Folder1/file1.1
All Files/Folder1/file1.2
All Files/Folder2/file2.1
All Files/Folder2/file2.2
All Files/file1
All Files/file2 -
Indeed that is probably true. I no longer have my test setup but I believe something like this would likely fix the bug you have identified:
import os
import boxsdk
auth = boxsdk.OAuth2(
client_id=config['boxAppSettings']['clientID'],
client_secret='',
access_token=access_token
)
client = boxsdk.Client(auth)
def get_recursive(dir_list):
for item in dir_list[-1].get_items():
file_info = item.get()
yield "/".join([x.get().name for x in dir_list]) + "/" + item.get().name
if file_info.type == "folder":
dir_list.append(item)
yield from get_recursive(dir_list)
dir_list.pop()
print(os.linesep.join(list(get_recursive([client.folder('0')]))))
サインインしてコメントを残してください。
コメント
6件のコメント