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

  • 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)
Output:
$ ./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 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):
    click.echo(filename, filepath)


if __name__ == "__main__":
    cli()
Output:
$ ./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

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

Test dependencies

Documentation

https://pathvalidate.rtfd.io/

Project details


Release history Release notifications | RSS feed

This version

1.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pathvalidate-1.0.0.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

pathvalidate-1.0.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pathvalidate-1.0.0.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for pathvalidate-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c63e2fdd5365e78f579250f15c03ac45f95d1b8094dbcbe5d2766931ce5870ea
MD5 69949e57f123385e7acb98a73c0219c4
BLAKE2b-256 ebc333f90015b0bbd55d09add2916c56c4042e7f00ed64262fe43a8d7c497369

See more details on using hashes here.

File details

Details for the file pathvalidate-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: pathvalidate-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 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/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for pathvalidate-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40da73ded2e2c7a68567bb03efe27f86ef0661e69ad9db992c166d1a4b25a436
MD5 3291876c5aaa39a6e343163d8b426238
BLAKE2b-256 e0b335c08eec9d4c27cef905d1f49e864ba852ff87f3959f9ac7eb717a2a1cb6

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