function create_folder
Creates a nested folder structure on a FileCloud server by traversing a path and creating missing directories.
/tf/active/vicechatdev/filecloud_wuxi_sync.py
12 - 44
moderate
Purpose
This function recursively creates folders on a FileCloud server (vicebio.com) by walking through a given path. It checks if each directory level exists using the FileCloud API, and creates the entire remaining path structure when it encounters a missing directory. The function is designed to work with an authenticated session object and uses FileCloud's REST API endpoints for directory listing and creation.
Source Code
def create_folder(start_path,s):
path_elements=start_path[1:].split('/')
walking=True
for i,p in enumerate(path_elements[3:]):
if walking:
ServerURL='https://filecloud.vicebio.com/' #To be defined
infoEndPoint = 'core/getfilelist'
ApiParams = {'path': '/'+'/'.join(path_elements[:i+3])}
#print(ApiParams)
InfoCall=s.post(ServerURL+infoEndPoint, params=ApiParams, cookies = s.cookies)
doc = xmltodict.parse(InfoCall.text)
found=False
try:
for e in doc['entries']['entry']:
if e['name']==p:
found=True
except:
pass
if found:
## We found this dir entry so moving one down
pass
else:
## We need to create this directory all the way down
createEndPoint = 'core/createfolder'
## Upload API params.
ApiParams = {'path': '/'+'/'.join(path_elements[:i+3]),'subpath': "/".join(path_elements[i+3:])}
print("ready to create ",ApiParams)
createCall=s.post(ServerURL+createEndPoint, params=ApiParams, cookies = s.cookies)
if createCall.text == 'OK':
print('folder creation successfull.')
else:
print('folder creation failed.')
walking=False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
start_path |
- | - | positional_or_keyword |
s |
- | - | positional_or_keyword |
Parameter Details
start_path: A string representing the full path where folders should be created. Expected format is '/root/level1/level2/...' with forward slashes. The path should start with '/' and contain at least 3 initial path elements before the directories to be created.
s: A requests.Session object that maintains authentication cookies for FileCloud API calls. This session must be pre-authenticated with valid credentials before calling this function.
Return Value
This function does not return any value (implicitly returns None). It prints status messages to stdout indicating whether folder creation was successful or failed. Success is indicated by 'folder creation successfull.' and failure by 'folder creation failed.'
Dependencies
requestsxmltodict
Required Imports
import requests
import xmltodict
Usage Example
import requests
import xmltodict
# Create and authenticate session
s = requests.Session()
# Authenticate with FileCloud (authentication code not shown)
# s.post('https://filecloud.vicebio.com/core/login', data={'username': 'user', 'password': 'pass'})
# Create folder structure
start_path = '/root/parent/child/new_folder/subfolder'
create_folder(start_path, s)
# The function will check if /root/parent/child exists
# and create new_folder/subfolder if they don't exist
Best Practices
- Ensure the session object 's' is properly authenticated before calling this function
- The start_path must have at least 3 path elements before the directories to be created (due to path_elements[3:] slicing)
- Handle network errors and API failures externally as this function only prints status messages
- The hardcoded ServerURL should ideally be parameterized for better reusability
- Consider adding error handling for XML parsing failures and network timeouts
- The function stops walking after creating the first missing directory level and creates all remaining subdirectories at once
- Be aware that the function has a typo in the success message ('successfull' instead of 'successful')
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_folder_hierarchy 71.3% similar
-
function create_folder_hierarchy_v1 71.3% similar
-
function test_filecloud_operations 69.0% similar
-
function create_folder_hierarchy_v2 67.5% similar
-
function ensure_path_exists 64.3% similar