1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
.. contents:: **pathvalidate**
:backlinks: top
:depth: 2
Summary
=========
`pathvalidate <https://github.com/thombashi/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
- Sanitize will do:
- Remove invalid characters for a target platform
- Replace reserved names for a target platform
- Normalize
- Remove unprintable characters
- Argument validator/sanitizer for ``argparse`` and ``click``
- Multi platform support:
- ``Linux``
- ``Windows``
- ``macOS``
- ``POSIX``: POSIX-compliant systems (Linux, macOS, etc.)
- ``universal``: platform independent
- Multibyte character support
CLI tool
---------
You can find this package's command line interface tool at the `pathvalidate-cli <https://github.com/thombashi/pathvalidate-cli>`__ repository.
Examples
==========
Sanitize a filename
---------------------
:Sample Code:
.. code-block:: python
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:
.. code-block::
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:
.. code-block:: python
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:
.. code-block::
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:
.. code-block:: python
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:
.. code-block::
[PV1100] invalid characters found: platform=universal, description=invalids=('/'), value='fi:l*e/p"a?t>h|.t<xt'
[PV1002] found a reserved name by a platform: 'COM1' is a reserved name, platform=universal, reusable_name=False
Check a filename
------------------
:Sample Code:
.. code-block:: python
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:
.. code-block::
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:
.. code-block:: python
from argparse import ArgumentParser
from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg
parser = ArgumentParser()
parser.add_argument("--filename", type=validate_filename_arg)
parser.add_argument("--filepath", type=validate_filepath_arg)
options = parser.parse_args()
if options.filename:
print(f"filename: {options.filename}")
if options.filepath:
print(f"filepath: {options.filepath}")
:Output:
.. code-block::
$ ./examples/argparse_validate.py --filename eg
filename: eg
$ ./examples/argparse_validate.py --filename e?g
usage: argparse_validate.py [-h] [--filename FILENAME] [--filepath FILEPATH]
argparse_validate.py: error: argument --filename: [PV1100] invalid characters found: invalids=(':'), value='e:g', platform=Windows
.. note::
``validate_filepath_arg`` consider ``platform`` as of ``"auto"`` if the input is an absolute file path.
filename/filepath sanitizer for ``argparse``
----------------------------------------------
:Sample Code:
.. code-block:: python
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:
.. code-block::
$ ./examples/argparse_sanitize.py --filename e/g
filename: eg
.. note::
``sanitize_filepath_arg`` is set platform as ``"auto"``.
filename/filepath validator for ``click``
-------------------------------------------
:Sample Code:
.. code-block:: python
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: str, filepath: str) -> None:
if filename:
click.echo(f"filename: {filename}")
if filepath:
click.echo(f"filepath: {filepath}")
if __name__ == "__main__":
cli()
:Output:
.. code-block::
$ ./examples/click_validate.py --filename ab
filename: ab
$ ./examples/click_validate.py --filepath e?g
Usage: click_validate.py [OPTIONS]
Try 'click_validate.py --help' for help.
Error: Invalid value for '--filename': [PV1100] invalid characters found: invalids=('?'), value='e?g', platform=Windows
filename/filepath sanitizer for ``click``
-------------------------------------------
:Sample Code:
.. code-block:: python
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(f"filename: {filename}")
if filepath:
click.echo(f"filepath: {filepath}")
if __name__ == "__main__":
cli()
:Output:
.. code-block::
$ ./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 conda-forge::pathvalidate
Installation: apt
------------------------------
::
sudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-pathvalidate
Dependencies
============
Python 3.9+
no external dependencies.
Documentation
===============
https://pathvalidate.rtfd.io/
Sponsors
====================================
`Become a sponsor <https://github.com/sponsors/thombashi>`__
|