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
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(e, file=sys.stderr)
- Output:
invalid char found: invalid-char=':, \*, /, ", \?, >, \|, <', value='fi:l*e/p"a?t>h|.t<xt', reason=ErrorReason.INVALID_CHARACTER
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
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
pathvalidate-0.28.1.tar.gz
(18.2 kB
view hashes)
Built Distribution
Close
Hashes for pathvalidate-0.28.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c7cc9f93b5f8e14095ca9bfe81d3981a3b673d05c2ee9f412ea20ed4be8855d |
|
MD5 | 6126f5dd6f523c424cf32bf865328a09 |
|
BLAKE2b-256 | 5ae30fc0e098c905b19e80fe1a290b16837d3208dcee8892fcffaa0e99deddc1 |