### Replace Symbols Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/sanitize.rst Demonstrates the usage of the replace_symbol function, which replaces specified symbols within a string. ```python from pathvalidate import replace_symbol string = replace_symbol('abc<>:|"?*123', '_') print(string) ``` -------------------------------- ### Sanitize Filepath Source: https://github.com/thombashi/pathvalidate/blob/master/examples/pathvalidate_examples.ipynb Explains how to use `sanitize_filepath` to clean file paths by removing or replacing characters that are not allowed in file paths. Shows examples with various problematic characters. ```python from pathvalidate import sanitize_filepath fpath = 'fi:l*e/p"a?t>h|.t {sanitize_filepath(fpath)}\n") fpath = "\0_a*b:ce%f/(g)h+i_0.txt" print(f"{fpath} -> {sanitize_filepath(fpath)}\n") ``` -------------------------------- ### Sanitize Filepath Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/sanitize.rst Demonstrates the usage of the sanitize_filepath function, which replaces invalid characters in a filepath. ```python from pathvalidate import sanitize_filepath filepath = sanitize_filepath('dir/../file<>:|"?*.txt') print(filepath) ``` -------------------------------- ### Custom Reserved Name Handler for Filename Sanitization Source: https://github.com/thombashi/pathvalidate/blob/master/examples/pathvalidate_examples.ipynb Illustrates how to provide a custom handler function to `sanitize_filename` for managing reserved names. This example appends an underscore to reserved names. ```python from pathvalidate import sanitize_filename, ValidationError def add_trailing_underscore(e: ValidationError) -> str: if e.reusable_name: return e.reserved_name return f"{e.reserved_name}_" sanitize_filename( ".", reserved_name_handler=add_trailing_underscore, additional_reserved_names=["."] ) ``` -------------------------------- ### Sanitize Filename Source: https://github.com/thombashi/pathvalidate/blob/master/examples/pathvalidate_examples.ipynb Demonstrates the `sanitize_filename` function to remove or replace invalid characters from filenames, making them safe for use. Includes examples with different types of invalid characters. ```python from pathvalidate import sanitize_filename fname = 'fi:l*e/p"a?t>h|.t {sanitize_filename(fname)}\n") fname = "\0_a*b:ce%f/(g)h+i_0.txt" print(f"{fname} -> {sanitize_filename(fname)}\n") ``` -------------------------------- ### Sanitize Filename Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/sanitize.rst Demonstrates the usage of the sanitize_filename function, which replaces invalid characters in a filename. ```python from pathvalidate import sanitize_filename filepath = sanitize_filename('file<>":|?*.txt') print(filepath) ``` -------------------------------- ### Validate Filename and Filepath with Argparse in Python Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/argparse_validator.txt This Python code snippet showcases how to use `validate_filename_arg` and `validate_filepath_arg` from the pathvalidate library with Python's `ArgumentParser`. It defines arguments for filename and filepath and prints them if provided. The validation ensures that invalid characters are caught during argument parsing. ```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}") ``` -------------------------------- ### Sanitize Filename and Filepath with Argparse Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/argparse_sanitizer.txt This Python code uses the pathvalidate library to sanitize command-line arguments for filenames and filepaths. It defines an ArgumentParser, adds arguments with custom sanitization types, and prints the sanitized values. The sanitize_filepath_arg is set to 'auto' for platform detection. ```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)) ``` -------------------------------- ### Validate Filename with Invalid Characters and Reserved Names (Python) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/validate_filename_code.txt This snippet shows how to use the `validate_filename` function from the `pathvalidate` library to check for invalid characters and reserved names in a filename. It includes examples of catching `ValidationError` exceptions and printing error messages to standard error. ```python import sys from pathvalidate import ValidationError, validate_filename try: validate_filename("fi:l*e/p\"a?t>h|.th|.t None: if filename: click.echo(f"filename: {filename}") if filepath: click.echo(f"filepath: {filepath}") if __name__ == "__main__": cli() ``` -------------------------------- ### Sanitize Filename and Filepath (Default Behavior) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/tips.rst Demonstrates the default behavior of `sanitize_filename` and `sanitize_filepath` when processing dot-files and dot-directories. `sanitize_filename` leaves '.' unchanged, while `sanitize_filepath` normalizes paths like 'hoge/./foo' to 'hoge/foo'. ```python print(sanitize_filename(".")) print(sanitize_filepath("hoge/./foo")) ``` ```console . hoge/foo ``` -------------------------------- ### Validate and Sanitize File Path in Python Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/is_valid_filepath_code.txt This snippet shows how to use the `is_valid_filepath` and `sanitize_filepath` functions from the `pathvalidate` library. It checks an initial invalid path, then sanitizes it and checks the sanitized path for validity. Dependencies include the `pathvalidate` library. ```python from pathvalidate import is_valid_filepath, sanitize_filepath fpath = "fi:l*e/p\"a?t>h|.t str: if e.reusable_name: return e.reserved_name return f"{e.reserved_name}_" print( sanitize_filename( ".", reserved_name_handler=always_add_trailing_underscore, additional_reserved_names=[".", ".."], ) ) print( sanitize_filepath( "hoge/./foo", normalize=False, reserved_name_handler=always_add_trailing_underscore, additional_reserved_names=[".", ".."], ) ) ``` -------------------------------- ### Replace Symbol in Filename Source: https://github.com/thombashi/pathvalidate/blob/master/examples/pathvalidate_examples.ipynb Illustrates the `replace_symbol` function, which replaces all symbols in a string with an empty string, effectively removing them. Useful for cleaning filenames. ```python from pathvalidate import replace_symbol name = "\0_a*b:ce%f/(g)h+i_0.txt" print(f"{name} -> {replace_symbol(name)}") ``` -------------------------------- ### Validate Filepath with Invalid Characters Source: https://github.com/thombashi/pathvalidate/blob/master/examples/pathvalidate_examples.ipynb Illustrates the use of `validate_filepath` to check for invalid characters within a file path, specifically for the Windows platform. It catches and prints `ValidationError`. ```python import sys from pathvalidate import ValidationError, validate_filepath try: validate_filepath('fi:l*e/p"a?t>h|.th|.t {sanitize_filename(fname)}\n") fname = "\0_a*b:ce%f/(g)h+i_0.txt" print(f"{fname} -> {sanitize_filename(fname)}\n") ``` -------------------------------- ### Validate filename and filepath arguments with argparse Source: https://github.com/thombashi/pathvalidate/blob/master/README.rst This snippet shows how to use pathvalidate's `validate_filename_arg` and `validate_filepath_arg` with argparse to validate command-line arguments. It handles cases with valid and invalid inputs, demonstrating error messages for disallowed characters. ```python from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg from argparse import ArgumentParser 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}") ``` -------------------------------- ### pathvalidate Error Reasons Enum Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/error.rst This snippet documents the ErrorReason enumeration from the pathvalidate library, listing its members and their inheritance. It provides a structured way to understand the reasons for validation failures. ```python class pathvalidate.error.ErrorReason: """Enum for reasons why a path is invalid.""" NULL_NAME = "the value must not be an empty string" RESERVED_NAME = "found a reserved name by a platform" INVALID_CHARACTER = "invalid characters found" INVALID_LENGTH = "found an invalid string length" FOUND_ABS_PATH = "found an absolute path where must be a relative path" MALFORMED_ABS_PATH = "found a malformed absolute path" INVALID_AFTER_SANITIZE = "found invalid value after sanitizing" ``` -------------------------------- ### Validate and Sanitize Filename in Python Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/examples/is_valid_filename_code.txt Demonstrates using `is_valid_filename` to check filename validity and `sanitize_filename` to remove invalid characters. Requires the pathvalidate library. ```python from pathvalidate import is_valid_filename, sanitize_filename fname = "fi:l*e/p\"a?t>h|.th|.t {sanitize_filepath(fpath)}\n") fpath = "\0_a*b:ce%f/(g)h+i_0.txt" print(f"{fpath} -> {sanitize_filepath(fpath)}\n") ``` -------------------------------- ### Validate and Sanitize Filename (Python) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/function.rst Validates if a given string is a valid filename and sanitizes it by removing or replacing invalid characters. It handles platform-specific filename rules. No external dependencies are required. ```Python from pathvalidate import validate_filename, sanitize_filename # Example usage filename = "my_file.txt" try: validate_filename(filename) print(f'"{filename}" is a valid filename.') except Exception as e: print(f'Invalid filename: {e}') sanitized_filename = sanitize_filename(filename) print(f'Sanitized filename: "{sanitized_filename}"') ``` -------------------------------- ### Validate and Replace Symbols (Python) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/function.rst Validates a string for the presence of invalid symbols and replaces them with a specified character or string. Useful for creating cross-platform compatible strings. No external libraries are required. ```Python from pathvalidate import validate_symbol, replace_symbol # Example usage symbol_string = "file?name*withsymbols" try: validate_symbol(symbol_string) print(f'"{symbol_string}" contains valid symbols.') except Exception as e: print(f'Invalid symbol found: {e}') replaced_string = replace_symbol(symbol_string, replacement='_') print(f'String with replaced symbols: "{replaced_string}"') ``` -------------------------------- ### Validate and Sanitize Filepath (Python) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/function.rst Validates if a given string represents a valid file path and sanitizes it by removing or replacing invalid characters. It considers platform-specific path constraints. No external dependencies are needed. ```Python from pathvalidate import validate_filepath, sanitize_filepath # Example usage filepath = "/path/to/my_document.txt" try: validate_filepath(filepath) print(f'"{filepath}" is a valid filepath.') except Exception as e: print(f'Invalid filepath: {e}') sanitized_filepath = sanitize_filepath(filepath) print(f'Sanitized filepath: "{sanitized_filepath}"') ``` -------------------------------- ### Check Filepath Validity (Python) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/function.rst Checks whether a given string is a valid file path according to system rules. This function only verifies the path's format and does not check for the existence of the file or directory. It raises an exception for invalid paths. ```Python from pathvalidate import is_valid_filepath # Example usage filepath1 = "/usr/local/bin" filepath2 = "C:\\Program Files\\App\0" print(f'Is "{filepath1}" valid? {is_valid_filepath(filepath1)}') print(f'Is "{filepath2}" valid? {is_valid_filepath(filepath2)}') ``` -------------------------------- ### Check Filename Validity (Python) Source: https://github.com/thombashi/pathvalidate/blob/master/docs/pages/reference/function.rst Checks if a given string conforms to the rules of a valid filename. This function does not modify the filename. It raises an exception if the filename is invalid. ```Python from pathvalidate import is_valid_filename # Example usage filename1 = "document.pdf" filename2 = "$pecial_file*.doc" print(f'Is "{filename1}" valid? {is_valid_filename(filename1)}') print(f'Is "{filename2}" valid? {is_valid_filename(filename2)}') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.