function sanitize_folders
Recursively traverses a directory tree and sanitizes folder names by removing non-ASCII characters, renaming folders to ASCII-only versions.
/tf/active/vicechatdev/creation_updater.py
9 - 27
moderate
Purpose
This function is designed to clean up directory structures by ensuring all folder names contain only ASCII characters. It recursively walks through a directory tree starting from the given path, identifies folders with non-ASCII characters, and renames them by stripping out non-ASCII characters. This is useful for ensuring compatibility with systems that don't handle Unicode filenames well, or for standardizing directory naming conventions.
Source Code
def sanitize_folders(path):
try:
os.chdir(path)
for entry in os.scandir(path):
try:
os.chdir(entry.name)
print(entry.name)
sanitize_folders(path+"/"+entry.name)
os.chdir(path)
name_clean=bytes(entry.name, 'utf-8').decode('ascii', 'ignore')
#print(entry.name,name_clean)
if entry.name!=name_clean:
os.rename(entry.name,name_clean)
except:
pass
except Exception as e:
print(e)
pass
return
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
path |
- | - | positional_or_keyword |
Parameter Details
path: String representing the absolute or relative file system path to the root directory where folder sanitization should begin. The function will recursively process all subdirectories from this starting point. Expected to be a valid directory path that exists and is accessible.
Return Value
Returns None (implicit return). The function performs in-place modifications to the file system by renaming directories. It does not return any value or status information about the operations performed.
Dependencies
os
Required Imports
import os
Usage Example
import os
# Create a test directory structure
test_path = '/tmp/test_folders'
os.makedirs(test_path, exist_ok=True)
os.makedirs(os.path.join(test_path, 'folder_with_émojis'), exist_ok=True)
# Sanitize the folders
sanitize_folders(test_path)
# The folder 'folder_with_émojis' will be renamed to 'folder_with_mojis'
# (non-ASCII characters removed)
Best Practices
- CAUTION: This function modifies the file system in-place and can break references to renamed directories
- Always backup important data before running this function as it permanently renames directories
- The function silently catches all exceptions with bare 'except' clauses, which may hide critical errors
- The function changes the current working directory (os.chdir), which can cause unexpected behavior in multi-threaded applications
- Consider using os.path.join() instead of string concatenation with '/' for better cross-platform compatibility
- The function prints directory names during execution, which may produce excessive output for large directory trees
- Empty folder names may result if all characters are non-ASCII, potentially causing errors
- The function does not handle file name sanitization, only folder names
- Race conditions may occur if other processes are modifying the directory structure simultaneously
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function sanitize_filename 64.5% similar
-
function capitalize_unicode_name 58.5% similar
-
class sanitize_identifier_fn 53.4% similar
-
function clean_text 49.8% similar
-
function clean_html_tags 48.6% similar