pathvalidate
pathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc.
What it is and what it does
pathvalidate is a Python library that cleans and validates filenames and file paths by removing or replacing characters that are invalid, reserved, or unprintable on target platforms. It handles platform-specific rules for Linux, Windows, macOS, POSIX, and universal (cross-platform) contexts, and includes support for multibyte characters. The library provides both standalone functions and integrations with argparse and click for command-line argument validation and sanitization.
You use it when you need to accept user input or external data as filenames or paths and ensure they will work on your target platform without errors. It removes problematic characters, replaces reserved names (like COM1 on Windows), normalizes whitespace, and strips unprintable characters—all without external dependencies.
Use it for:
- Sanitize user-supplied filenames in web applications or file upload handlers before writing to disk.
- Validate file paths in CLI tools using argparse or click to reject invalid input early.
- Clean batch-imported filenames from external sources (APIs, databases, archives) for cross-platform compatibility.
- Normalize path strings in data pipelines to prevent filesystem errors across development, staging, and production environments.
- Check whether a string is a valid filename before attempting file operations to provide clear error messages.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Sanitize and validate filenames and file paths by removing or replacing invalid characters, reserved names, and unprintable characters across multiple platforms (Linux, Windows, macOS, POSIX, and universal).
Yes. pathvalidate is a focused, well-maintained library with zero dependencies, permissive licensing, and active development. Install it if you need reliable filename or path validation across platforms; the low friction and clear API make it a straightforward addition to any project handling file I/O.
Install
pathvalidate on PyPI
pip
pip install pathvalidateuv
uv add pathvalidatepoetry
poetry add pathvalidateInstalling pathvalidate
Before you install
Low friction: zero runtime dependencies, pure Python wheel distribution. Actively maintained with recent commits and stable production status.
License in practice
MIT License (permissive): you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.
Quickstart
pip install pathvalidate
from pathvalidate import sanitize_filename, validate_filename
fname = "fi:l*e/p\"a?t>h|.t<xt"
print(sanitize_filename(fname)) # Returns valid filename
validate_filename(fname) # Raises ValidationError if invalid
Requires Python 3.9 or later.
Verify before relying
- Whether multibyte character support covers all Unicode normalization edge cases across platforms.
- Performance characteristics when sanitizing very long paths or batch processing large filename lists.
Package facts
| License | MIT License (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 425 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 20,577,101/month — #1,032 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pathvalidate-3.3.1-py3-none-any.whl
Keywords: file, path, validation, validator, sanitization, sanitizer
Tags
More Libraries packages
urllib3 is an HTTP client library that provides…
permissive · top 100 on PyPI
requestsRequests is a Python HTTP library that…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
python-dateutilProvides parsing, arithmetic, and recurrence…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
pytestpytest is a testing framework that lets you…
permissive · top 100 on PyPI
sanitize-filenameRemoves or replaces problematic characters from…
permissive · top 15,000 on PyPI
msgfyConverts Python Exception instances into…
permissive · top 15,000 on PyPI
html-sanitizerSanitizes HTML fragments by enforcing an…
permissive · top 5,000 on PyPI
pathtoolsProvides pattern matching and utility functions…
permissive · top 15,000 on PyPI
iregexp-checkValidates regular expressions against RFC 9485…
permissive · top 15,000 on PyPI
eradicateEradicate detects and removes commented-out…
permissive · top 5,000 on PyPI
wheel-filenameParse and validate wheel filenames according to…
permissive · top 15,000 on PyPI
nh3nh3 sanitizes HTML by removing unsafe tags and…
permissive · top 1,000 on PyPI
pgsanityPgSanity validates PostgreSQL SQL syntax by…
permissive · top 15,000 on PyPI
strip-ansiRemoves ANSI escape sequences (color codes,…
unclear · top 15,000 on PyPI