🔍 Code Extractor

function sanitize_folders

Maturity: 38

Recursively traverses a directory tree and sanitizes folder names by removing non-ASCII characters, renaming folders to ASCII-only versions.

File:
/tf/active/vicechatdev/creation_updater.py
Lines:
9 - 27
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function sanitize_filename 64.5% similar

    Sanitizes a filename string by replacing invalid filesystem characters with underscores and ensuring a valid output.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function capitalize_unicode_name 58.5% similar

    Transforms Unicode character name strings by removing the word 'capital' and capitalizing the following word, converting strings like 'capital delta' to 'Delta'.

    From: /tf/active/vicechatdev/patches/util.py
  • class sanitize_identifier_fn 53.4% similar

    A parameterized function class that sanitizes strings (group/label values) to make them safe for use as Python attribute names in AttrTree structures by converting special characters to their unicode names and applying transformations.

    From: /tf/active/vicechatdev/patches/util.py
  • function clean_text 49.8% similar

    Cleans and normalizes text content by removing HTML tags, normalizing whitespace, and stripping markdown formatting elements.

    From: /tf/active/vicechatdev/improved_convert_disclosures_to_table.py
  • function clean_html_tags 48.6% similar

    Removes HTML tags and entities from text strings, returning clean plain text suitable for PDF display or other formatted output.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse