Filename Sanitizer

1.2.0 · maintenance · verified Thu Apr 16

sanitize-filename is a simple, dependency-free, blacklist-based filename sanitizer for Python. It focuses on preserving the original filename as much as possible, including non-ASCII characters, while removing characters unsafe for common file systems. The current version is 1.2.0, released in April 2020. It's a stable library with infrequent updates, primarily for minor fixes and behavior uniformity.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the `sanitize` function to clean up a filename string, removing characters that are typically invalid or problematic across various file systems.

from sanitize_filename import sanitize

# Example usage
unsafe_filename = 'My/Document:with"illegal*chars?.txt'
safe_filename = sanitize(unsafe_filename)
print(f"Original: {unsafe_filename}")
print(f"Sanitized: {safe_filename}")

# Another example with reserved names or paths
unsafe_path = '../etc/passwd'
safe_path = sanitize(unsafe_path)
print(f"Original: {unsafe_path}")
print(f"Sanitized: {safe_path}")

view raw JSON →