pathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc.
Project description
Summary
pathvalidate is a Python library to sanitize/validate a string such as filenames/file-paths/etc.
Features
- Sanitize/Validate a string as a:
file name
file path
file name/path argument validator/sanitizer for argparse and click
- Multi platform support:
sanitize/validate file names/paths for a specific platform (Linux/Windows/macOS/Posix) or universal (platform independent)
Multibyte character support
Examples
Sanitize a filename
- Sample Code:
from pathvalidate import sanitize_filename fname = "fi:l*e/p\"a?t>h|.t<xt" print(f"{fname} -> {sanitize_filename(fname)}\n") fname = "\0_a*b:c<d>e%f/(g)h+i_0.txt" print(f"{fname} -> {sanitize_filename(fname)}\n")- Output:
fi:l*e/p"a?t>h|.t<xt -> filepath.txt _a*b:c<d>e%f/(g)h+i_0.txt -> _abcde%f(g)h+i_0.txt
The default target platform is universal. i.e. the sanitized file name is valid for any platform.
Sanitize a filepath
- Sample Code:
from pathvalidate import sanitize_filepath fpath = "fi:l*e/p\"a?t>h|.t<xt" print(f"{fpath} -> {sanitize_filepath(fpath)}\n") fpath = "\0_a*b:c<d>e%f/(g)h+i_0.txt" print(f"{fpath} -> {sanitize_filepath(fpath)}\n")- Output:
fi:l*e/p"a?t>h|.t<xt -> file/path.txt _a*b:c<d>e%f/(g)h+i_0.txt -> _abcde%f/(g)h+i_0.txt
Validate a filename
- Sample Code:
import sys from pathvalidate import ValidationError, validate_filename try: validate_filename("fi:l*e/p\"a?t>h|.t<xt") except ValidationError as e: print(f"{e}\n", file=sys.stderr) try: validate_filename("COM1") except ValidationError as e: print(f"{e}\n", file=sys.stderr)- Output:
invalid char found: invalids=(':', '*', '/', '"', '?', '>', '|', '<'), value='fi:l*e/p"a?t>h|.t<xt', reason=INVALID_CHARACTER, target-platform=Windows 'COM1' is a reserved name, reason=RESERVED_NAME, target-platform=universal
Check a filename
- Sample Code:
from pathvalidate import is_valid_filename, sanitize_filename fname = "fi:l*e/p\"a?t>h|.t<xt" print(f"is_valid_filename('{fname}') return {is_valid_filename(fname)}\n") sanitized_fname = sanitize_filename(fname) print(f"is_valid_filename('{sanitized_fname}') return {is_valid_filename(sanitized_fname)}\n")- Output:
is_valid_filename('fi:l*e/p"a?t>h|.t<xt') return False is_valid_filename('filepath.txt') return True
filename/filepath validator for argparse
- Sample Code:
from argparse import ArgumentParser from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg parser = ArgumentParser() parser.add_argument("--filepath", type=validate_filepath_arg) parser.add_argument("--filename", type=validate_filename_arg) options = parser.parse_args() if options.filename: print("filename: {}".format(options.filename)) if options.filepath: print("filepath: {}".format(options.filepath))- Output:
$ ./examples/argparse_validate.py --filename eg filename: eg $ ./examples/argparse_validate.py --filepath e?g usage: argparse_validate.py [-h] [--filepath FILEPATH] [--filename FILENAME] argparse_validate.py: error: argument --filepath: invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-platform=Windows
filename/filepath sanitizer for argparse
- Sample Code:
from argparse import ArgumentParser from pathvalidate.argparse import sanitize_filename_arg, sanitize_filepath_arg parser = ArgumentParser() parser.add_argument("--filename", type=sanitize_filename_arg) parser.add_argument("--filepath", type=sanitize_filepath_arg) options = parser.parse_args() if options.filename: print("filename: {}".format(options.filename)) if options.filepath: print("filepath: {}".format(options.filepath))- Output:
$ ./examples/argparse_sanitize.py --filename e/g filename: eg
filename/filepath validator for click
- Sample Code:
import click from pathvalidate.click import validate_filename_arg, validate_filepath_arg @click.command() @click.option("--filename", callback=validate_filename_arg) @click.option("--filepath", callback=validate_filepath_arg) def cli(filename, filepath): if filename: click.echo("filename: {}".format(filename)) if filepath: click.echo("filepath: {}".format(filepath)) if __name__ == "__main__": cli()- Output:
$ ./examples/click_validate.py --filename ab filename: ab $ ./examples/click_validate.py --filepath e?g Usage: click_validate.py [OPTIONS] Error: Invalid value for "--filepath": invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-platform=Windows
filename/filepath sanitizer for click
- Sample Code:
import click from pathvalidate.click import sanitize_filename_arg, sanitize_filepath_arg @click.command() @click.option("--filename", callback=sanitize_filename_arg) @click.option("--filepath", callback=sanitize_filepath_arg) def cli(filename, filepath): if filename: click.echo("filename: {}".format(filename)) if filepath: click.echo("filepath: {}".format(filepath)) if __name__ == "__main__": cli()- Output:
$ ./examples/click_sanitize.py --filename a/b filename: ab
For more information
More examples can be found at https://pathvalidate.rtfd.io/en/latest/pages/examples/index.html
Installation
Installation: pip
pip install pathvalidate
Installation: conda
conda install -c thombashi pathvalidate
Installation: apt
sudo add-apt-repository ppa:thombashi/ppa sudo apt update sudo apt install python3-pathvalidate
Dependencies
Python 3.5+ No external dependencies.
Test dependencies
Documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pathvalidate-2.3.0.tar.gz.
File metadata
- Download URL: pathvalidate-2.3.0.tar.gz
- Upload date:
- Size: 25.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32d30dbacb711c16bb188b12ce7e9a46b41785f50a12f64500f747480a4b6ee3
|
|
| MD5 |
ee380a00148f44276c51069ce7e7d7c6
|
|
| BLAKE2b-256 |
b1dd09defaa975475b35d84d199e660981c327fcf1468656809984c3c106529a
|
File details
Details for the file pathvalidate-2.3.0-py3-none-any.whl.
File metadata
- Download URL: pathvalidate-2.3.0-py3-none-any.whl
- Upload date:
- Size: 18.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1697c8ea71ff4c48e7aa0eda72fe4581404be8f41e51a17363ef682dd6824d35
|
|
| MD5 |
2e20803a2228ceded83c88252fcba531
|
|
| BLAKE2b-256 |
aafa1a951084aa93940399800e37ed6f096ad5c0de3c26604be62f9464a39fc1
|