🔍 Code Extractor

function get_file_extension

Maturity: 53

Extracts and returns the file extension from a given filename string, normalized to lowercase without the leading dot.

File:
/tf/active/vicechatdev/CDocs/utils/__init__.py
Lines:
106 - 119
Complexity:
simple

Purpose

This utility function parses a filename to extract its extension, which is useful for file type validation, routing files to appropriate handlers, filtering files by type, or determining how to process uploaded documents. It handles edge cases like files without extensions and ensures consistent output by converting to lowercase.

Source Code

def get_file_extension(filename: str) -> str:
    """
    Get file extension from filename.
    
    Args:
        filename: Filename
        
    Returns:
        File extension (lowercase, without dot) or empty string if none
    """
    parts = filename.rsplit(".", 1)
    if len(parts) > 1:
        return parts[1].lower()
    return ""

Parameters

Name Type Default Kind
filename str - positional_or_keyword

Parameter Details

filename: A string representing the filename (with or without path). Can be a simple filename like 'document.pdf', a full path like '/path/to/file.txt', or a filename without extension like 'README'. The function will extract only the extension portion after the last dot.

Return Value

Type: str

Returns a string containing the file extension in lowercase without the leading dot (e.g., 'pdf', 'txt', 'docx'). If the filename has no extension (no dot present or dot is the last character), returns an empty string (''). The lowercase normalization ensures consistent comparison regardless of how the original filename was cased.

Usage Example

# Basic usage
extension = get_file_extension('document.PDF')
print(extension)  # Output: 'pdf'

# File without extension
extension = get_file_extension('README')
print(extension)  # Output: ''

# Full path
extension = get_file_extension('/home/user/files/report.docx')
print(extension)  # Output: 'docx'

# Multiple dots in filename
extension = get_file_extension('archive.tar.gz')
print(extension)  # Output: 'gz'

# Use in file validation
allowed_extensions = ['pdf', 'doc', 'docx']
filename = 'upload.pdf'
if get_file_extension(filename) in allowed_extensions:
    print('Valid file type')

# Use in routing logic
file_ext = get_file_extension('image.png')
if file_ext in ['jpg', 'jpeg', 'png', 'gif']:
    process_image(filename)
elif file_ext == 'pdf':
    process_pdf(filename)

Best Practices

  • The function uses rsplit with maxsplit=1 to handle filenames with multiple dots correctly, extracting only the final extension
  • Always returns lowercase for consistent comparison - remember to lowercase your comparison values as well
  • Returns empty string rather than None for no extension, making it safe to use in string operations without null checks
  • Does not validate if the extension is a 'real' file type - it simply extracts text after the last dot
  • For full path strings, the function works correctly as it only looks for the last dot in the entire string
  • Consider additional validation if you need to verify the extension is from a whitelist of allowed types
  • The function does not strip whitespace - ensure filenames are cleaned before passing if needed

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_mime_type 63.2% similar

    Determines the MIME type of a file based on its file extension by mapping common extensions to their corresponding MIME type strings.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function is_valid_document_file 56.2% similar

    Validates whether a given filename has an extension corresponding to a supported document type by checking against a predefined list of valid document extensions.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function sanitize_filename 54.1% 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 allowed_file 49.5% similar

    Validates whether a filename has an allowed file extension by checking if it contains a dot and if the extension (case-insensitive) exists in a predefined ALLOWED_EXTENSIONS collection.

    From: /tf/active/vicechatdev/leexi/app.py
  • function allowed_file_v1 42.5% similar

    Validates whether a given filename has an allowed file extension by checking if the extension exists in a configured whitelist.

    From: /tf/active/vicechatdev/full_smartstat/app.py
← Back to Browse