4.1. Sanitize a filename¶
The sanitize_filename()
function returns a filename which replaced invalid character(s)
for a filename within the argument.
- Sample Code:
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:
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.
4.2. Sanitize a filepath¶
The sanitize_filepath()
function returns a filepath which replaced invalid character(s)
for a filepath within the argument.
- Sample Code:
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:
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
4.3. Replace symbols¶
The replace_symbol()
function returns a string which replaced symbol(s) within the argument.
- Sample Code:
from pathvalidate import replace_symbol name = "\0_a*b:c<d>e%f/(g)h+i_0.txt" print(f"{name} -> {replace_symbol(name)}")
- Output:
_a*b:c<d>e%f/(g)h+i_0.txt -> abcdefghi0txt