Package: pathvalidate
pathvalidateSummaryFeaturesExamplesSanitize a filenameSanitize a filepathValidate a filenameCheck a filenamefilename/filepath validator forargparsefilename/filepath sanitizer forargparsefilename/filepath validator forclickfilename/filepath sanitizer forclickFor more informationInstallationInstallation: pipInstallation: condaInstallation: aptDependenciesDocumentationSponsorsSummarypathvalidateis a Python library to sanitize/validate a string such as filenames/file-paths/etc.FeaturesSanitize/Validate a string as a:file namefile pathSanitize will do:Remove invalid characters for a target platformReplace reserved names for a target platformNormalizeRemove unprintable charactersArgument validator/sanitizer forargparseandclickMulti platform support:LinuxWindowsmacOSPOSIXuniversal(platform independent)Multibyte character supportExamplesSanitize a filenameSample Code:frompathvalidateimportsanitize_filenamefname="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: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.txtThe default targetplatformisuniversal.
i.e. the sanitized file name is valid for any platform.Sanitize a filepathSample Code:frompathvalidateimportsanitize_filepathfpath="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: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.txtValidate a filenameSample Code:importsysfrompathvalidateimportValidationError,validate_filenametry:validate_filename("fi:l*e/p\"a?t>h|.t<xt")exceptValidationErrorase:print(f"{e}\n",file=sys.stderr)try:validate_filename("COM1")exceptValidationErrorase:print(f"{e}\n",file=sys.stderr)Output:[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=FalseCheck a filenameSample Code:frompathvalidateimportis_valid_filename,sanitize_filenamefname="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:is_valid_filename('fi:l*e/p"a?t>h|.t<xt') return False

is_valid_filename('filepath.txt') return Truefilename/filepath validator forargparseSample Code:fromargparseimportArgumentParserfrompathvalidate.argparseimportvalidate_filename_arg,validate_filepath_argparser=ArgumentParser()parser.add_argument("--filename",type=validate_filename_arg)parser.add_argument("--filepath",type=validate_filepath_arg)options=parser.parse_args()ifoptions.filename:print(f"filename:{options.filename}")ifoptions.filepath:print(f"filepath:{options.filepath}")Output:$ ./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=WindowsNotevalidate_filepath_argconsiderplatformas of"auto"if the input is an absolute file path.filename/filepath sanitizer forargparseSample Code:fromargparseimportArgumentParserfrompathvalidate.argparseimportsanitize_filename_arg,sanitize_filepath_argparser=ArgumentParser()parser.add_argument("--filename",type=sanitize_filename_arg)parser.add_argument("--filepath",type=sanitize_filepath_arg)options=parser.parse_args()ifoptions.filename:print("filename:{}".format(options.filename))ifoptions.filepath:print("filepath:{}".format(options.filepath))Output:$ ./examples/argparse_sanitize.py --filename e/g
filename: egNotesanitize_filepath_argis set platform as"auto".filename/filepath validator forclickSample Code:importclickfrompathvalidate.clickimportvalidate_filename_arg,validate_filepath_arg@click.command()@click.option("--filename",callback=validate_filename_arg)@click.option("--filepath",callback=validate_filepath_arg)defcli(filename:str,filepath:str)->None:iffilename:click.echo(f"filename:{filename}")iffilepath:click.echo(f"filepath:{filepath}")if__name__=="__main__":cli()Output:$ ./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=Windowsfilename/filepath sanitizer forclickSample Code:importclickfrompathvalidate.clickimportsanitize_filename_arg,sanitize_filepath_arg@click.command()@click.option("--filename",callback=sanitize_filename_arg)@click.option("--filepath",callback=sanitize_filepath_arg)defcli(filename,filepath):iffilename:click.echo(f"filename:{filename}")iffilepath:click.echo(f"filepath:{filepath}")if__name__=="__main__":cli()Output:$ ./examples/click_sanitize.py --filename a/b
filename: abFor more informationMore examples can be found athttps://pathvalidate.rtfd.io/en/latest/pages/examples/index.htmlInstallationInstallation: pippip install pathvalidateInstallation: condaconda install -c thombashi pathvalidateInstallation: aptsudo add-apt-repository ppa:thombashi/ppa
sudo apt update
sudo apt install python3-pathvalidateDependenciesPython 3.7+
no external dependencies.Documentationhttps://pathvalidate.rtfd.io/SponsorsBecome a sponsor