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
validafilename/filepath validator for argparse/click
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("{} -> {}".format(fname, sanitize_filename(fname))) fname = "\0_a*b:c<d>e%f/(g)h+i_0.txt" print("{} -> {}".format(fname, sanitize_filename(fname)))- 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
Sanitize a filepath
- Sample Code:
from pathvalidate import sanitize_filepath fpath = "fi:l*e/p\"a?t>h|.t<xt" print("{} -> {}".format(fpath, sanitize_filepath(fpath))) fpath = "\0_a*b:c<d>e%f/(g)h+i_0.txt" print("{} -> {}".format(fpath, sanitize_filepath(fpath)))- 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("{}\n".format(e), file=sys.stderr) try: validate_filename("COM1") except ValidationError as e: print("{}\n".format(e), 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("is_valid_filename('{}') return {}".format(fname, is_valid_filename(fname))) sanitized_fname = sanitize_filename(fname) print("is_valid_filename('{}') return {}".format(sanitized_fname, is_valid_filename(sanitized_fname)))- 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 filepath, filename parser = ArgumentParser() parser.add_argument("--filepath", type=filepath) parser.add_argument("--filename", type=filename)
filename/filepath validator for click
- Sample Code:
import click from pathvalidate.click import filename, filepath @click.command() @click.option("--filename", callback=filename) @click.option("--filepath", callback=filepath) def cli(filename, filepath): # do something
For more information
More examples can be found at https://pathvalidate.rtfd.io/en/latest/pages/examples/index.html
Installation
Install from PyPI
pip install pathvalidate
Install from PPA (for Ubuntu)
sudo add-apt-repository ppa:thombashi/ppa sudo apt update sudo apt install python3-pathvalidate
Dependencies
Python 2.7+ or 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-0.29.1.tar.gz.
File metadata
- Download URL: pathvalidate-0.29.1.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
254bad5e77e173107cbcd2fd4e8be5715c208a0ad36766846ef728dab74736ec
|
|
| MD5 |
2967090bc0220f90a443d91678723aca
|
|
| BLAKE2b-256 |
9bb73433d5aa46d0c284f1aba59c5b9df5f5f36f44b023a8b6f2e43d0c9cb354
|
File details
Details for the file pathvalidate-0.29.1-py2.py3-none-any.whl.
File metadata
- Download URL: pathvalidate-0.29.1-py2.py3-none-any.whl
- Upload date:
- Size: 14.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06fdb3e0b70745c18a0ddb46f0411274ed2abb6aea570bb7772d99a01b6f9d61
|
|
| MD5 |
9a95a656c51f86a330da5f2bf2e36c14
|
|
| BLAKE2b-256 |
3f6a8365e74217b48b1c1765dd2b3f3efc0a52bd72951f34d9225d267bccf99f
|