Skip to main content

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.

PyPI package version Supported Python versions Linux/macOS CI status Windows CI status Test coverage GitHub stars

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.4+ No external dependencies.

Test dependencies

Documentation

https://pathvalidate.rtfd.io/

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.27.2.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pathvalidate-0.27.2-py2.py3-none-any.whl (13.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pathvalidate-0.27.2.tar.gz.

File metadata

  • Download URL: pathvalidate-0.27.2.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pathvalidate-0.27.2.tar.gz
Algorithm Hash digest
SHA256 bfeb111d8c7340ac7990920b051c372f63ff5bb64a8f4b1ed42c46621c8232a2
MD5 5271b2c7b509dc92f64b5a09431ee5d8
BLAKE2b-256 d5d90777bd438cd5f0f1c918ee7d060a1d27af79bffde6c212365cc0ff3acdf5

See more details on using hashes here.

File details

Details for the file pathvalidate-0.27.2-py2.py3-none-any.whl.

File metadata

  • Download URL: pathvalidate-0.27.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for pathvalidate-0.27.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9a37d820fb1c0f77015ee2c38c716874812cb21b8cd7034f60f18d1455189ffc
MD5 2c494e8f4a6ecb28045cf0c7f2a14ef4
BLAKE2b-256 87d9248128152718f05b96fc8491f549effd32190cdad3c08b2f43b31bf0f4e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page