function get_file_extension
Extracts and returns the file extension from a given filename string, normalized to lowercase without the leading dot.
/tf/active/vicechatdev/CDocs/utils/__init__.py
106 - 119
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_mime_type 63.2% similar
-
function is_valid_document_file 56.2% similar
-
function sanitize_filename 54.1% similar
-
function allowed_file 49.5% similar
-
function allowed_file_v1 42.5% similar