Registry / serialization / pathvalidate

pathvalidate

JSON →
library3.3.1pypypi✓ verified 24d ago

pathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc. It provides functions to remove invalid characters, replace reserved names, and normalize paths for various operating systems (Linux, Windows, macOS, POSIX, universal). The current version is 3.3.1, and it maintains an active release cadence with regular updates and feature enhancements.

pip install pathvalidate
INSTALL
IMPORT
SIG · PATHVALIDATE
P
pathvalidate
serializationpythonv3.3.1
Install
1.6s avg
Import
46ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.3.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.048s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.044s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

validate_filename
from pathvalidate import validate_filename
sanitize_filename
from pathvalidate import sanitize_filename
ValidationError
from pathvalidate import ValidationError
sanitize_filepath
from pathvalidate import sanitize_filepath

This quickstart demonstrates how to validate a filename, and how to sanitize both filenames and file paths, handling potential `ValidationError` exceptions.

import sys from pathvalidate import ValidationError, validate_filename, sanitize_filename, sanitize_filepath # Example 1: Validate a filename try: validate_filename("my:file*name?.txt") print("Filename is valid.") except ValidationError as e: print(f"Validation error: {e}", file=sys.stderr) # Example 2: Sanitize a filename invalid_fname = "my:inval|id*file?.txt" sanitized_fname = sanitize_filename(invalid_fname) print(f"Sanitized filename '{invalid_fname}' -> '{sanitized_fname}'") # Example 3: Sanitize a filepath invalid_fpath = "/usr/loc:al/my<dir>/invalid?file.log" sanitized_fpath = sanitize_filepath(invalid_fpath) print(f"Sanitized filepath '{invalid_fpath}' -> '{sanitized_fpath}'")
Debug
Known issues
breakingDropped support for Python 3.7 and 3.8 in v3.2.2. Prior to that, v3.1.0 dropped Python 3.6 support. Users on older Python versions must upgrade to Python 3.9 or newer to use current versions of pathvalidate.
fix
Upgrade your Python environment to version 3.9 or higher.
affects: >=3.1.0, >=3.2.2
breakingThe return type of `ValidationError.reason` changed from `Optional[ErrorReason]` to `ErrorReason` in v3.1.0. Code expecting an `Optional` type for this attribute may need adjustments.
fix
Update type hints and logic to directly handle `ErrorReason` as the return type for `ValidationError.reason`.
affects: >=3.1.0
breakingIn v3.0.0, the `min_len` argument was removed from the constructors of `FileNameSanitizer` and `FilePathSanitizer`, and a `validator` argument was added. Additionally, `InvalidLengthError` was removed, with `ValidationError` now used for length-related issues.
fix
Review calls to `FileNameSanitizer` and `FilePathSanitizer` constructors, remove `min_len`, and consider using the new `validator` argument if needed. Catch `ValidationError` instead of `InvalidLengthError`.
affects: >=3.0.0
gotchaFor platform-specific validation or sanitization, you must explicitly specify the `platform` argument (e.g., 'Windows', 'Linux', 'macOS', 'POSIX') in functions like `validate_filename` or `sanitize_filename`. If omitted, it defaults to 'universal' (most restrictive) for filenames or 'auto' for file paths, which might not match your intended target environment's rules.
fix
Always pass the `platform` argument (e.g., `platform='Windows'`) to validation and sanitization functions if specific OS rules are required.
affects: All
deprecatedVersion 2.5.1 introduced `DeprecationWarning` for some functions. While specific functions are not detailed in release notes, users should pay attention to warnings during execution or linting.
fix
Check `DeprecationWarning` messages during development and update to the recommended alternative functions or methods.
affects: >=2.5.1
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pathvalidate'
The 'pathvalidate' package is not installed in the Python environment where the code is being run.
fix
Install the package using pip: `pip install pathvalidate`
ValidationError: [PV1100] invalid characters found
The string provided to `validate_filename()` or `validate_filepath()` contains characters that are illegal for filenames or file paths on the specified (or default universal) platform.
fix
Use `sanitize_filename()` or `sanitize_filepath()` to remove or replace invalid characters before validation, or ensure the input string contains only valid characters. 

```python
from pathvalidate import validate_filename, sanitize_filename, ValidationError

try:
    validate_filename('fi:l*e/p"a?t>h|.t<xt')
except ValidationError as e:
    print(e) # Output: [PV1100] invalid characters found: ...

# Fix:
sanitized_name = sanitize_filename('fi:l*e/p"a?t>h|.t<xt')
print(sanitized_name) # Output: filepath.txt
validate_filename(sanitized_name) # This will now pass
```
ValidationError: [PV1002] found a reserved name by a platform
The filename or filepath string provided to a validation function is a reserved name (e.g., 'CON', 'NUL', 'COM1') on the target operating system, which cannot be used as a filename.
fix
Use `sanitize_filename()` or `sanitize_filepath()` which automatically handles reserved names by default, or provide a different name. You can also specify a `reserved_name_handler` for custom behavior. 

```python
from pathvalidate import validate_filename, sanitize_filename, ValidationError

try:
    validate_filename('COM1', platform='Windows')
except ValidationError as e:
    print(e) # Output: [PV1002] found a reserved name by a platform: 'COM1'

# Fix:
sanitized_name = sanitize_filename('COM1', platform='Windows')
print(sanitized_name) # Output: _COM1
validate_filename(sanitized_name, platform='Windows') # This will now pass
```
ImportError: cannot import name 'sanitise_filename' from 'pathvalidate'
A common misspelling (using 's' instead of 'z') of the `sanitize_filename` function is attempted during import.
fix
Correct the spelling of the function name to `sanitize_filename` (using 'z').

```python
# Wrong:
# from pathvalidate import sanitise_filename

# Correct:
from pathvalidate import sanitize_filename

filename = 'my/file.txt'
sanitized = sanitize_filename(filename)
print(f'{filename} -> {sanitized}')
```
Upgrade
Version history
3.3.1latest on PyPI · released Jun 15, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
pathvalidate — pip install pathvalidate · libregistry