Skip to content

Commit 9690a53

Browse files
committed
add a function to normalize the name of a path
1 parent 751a4cf commit 9690a53

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

aiutil/filesystem.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,25 @@ def _trace_dir_upwards(path: Path) -> Path:
718718
path = Path(path)
719719
prefix = _trace_dir_upwards(path)
720720
return PosixPathPair(prefix, path.relative_to(prefix))
721+
722+
723+
def normalize_path_name(
724+
path: str | Path, replacements: dict[str, str] | None = None
725+
) -> Path:
726+
"""Normalize the name of a path.
727+
728+
:param path: A path to be normalized.
729+
:param replacements: A mapping of characters to replace.
730+
"""
731+
if isinstance(path, str):
732+
path = Path(path)
733+
name = path.name
734+
if replacements is None:
735+
replacements = {
736+
" ": "_",
737+
"(": "_",
738+
")": "_",
739+
}
740+
path_new = path.with_name(name.translate(str.maketrans(replacements)))
741+
path.rename(path_new)
742+
return path_new

0 commit comments

Comments
 (0)