4.6. Check a filename¶
is_valid_filename()
function returns True
if a filename is valid for a specified platform.
- Sample Code:
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:
is_valid_filename('fi:l*e/p"a?t>h|.t<xt') return False is_valid_filename('filepath.txt') return True
4.7. Check a filepath¶
is_valid_filepath()
function returns True
if a filepath is valid for a specified platform.
- Sample Code:
from pathvalidate import is_valid_filepath, sanitize_filepath fpath = "fi:l*e/p\"a?t>h|.t<xt" print(f"is_valid_filepath('{fpath}') return {is_valid_filepath(fpath)}\n") sanitized_fpath = sanitize_filepath(fpath) print(f"is_valid_filepath('{sanitized_fpath}') return {is_valid_filepath(sanitized_fpath)}\n")
- Output:
is_valid_filepath('fi:l*e/p"a?t>h|.t<xt') return False is_valid_filepath('file/path.txt') return True