# 4.4. Validate a filename

The `validate_filename()` function raise `ValueError` if the name includes invalid character(s) for 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(f"{e}\n", file=sys.stderr)

      try:
          validate_filename("COM1")
      except ValidationError as e:
          print(f"{e}\n", 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

# 4.5. Validate a file path

The `validate_filepath()` function raise `ValueError` if the name includes invalid character(s) for a file path.

Sample Code

      import sys
      from pathvalidate import ValidationError, validate_filepath

      try:
          validate_filepath("fi:l*e/p\"a?t>h|.t<xt")
      except ValidationError as e:
          print(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