From d34103917f03cf12d6df50868f37dd24a8f75c2f Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 9 Jan 2022 16:59:09 -0800 Subject: [PATCH] Re-add the normalize_sep function, but more conservative. Formerly I was normalizing backslash to forwardslash on unix, which is bad. Then I removed the function because I thought normpath was the right tool for the job. Now I'm finding a few situations I just want to norm the sep again. --- voussoirkit/pathclass.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/voussoirkit/pathclass.py b/voussoirkit/pathclass.py index 7d1f8b7..2fa4e20 100644 --- a/voussoirkit/pathclass.py +++ b/voussoirkit/pathclass.py @@ -686,5 +686,22 @@ def normalize_pathpart(name): return name return PathPart(name) +def normalize_sep(path) -> str: + ''' + Normalize path separators as appropriate for the operating system. + + On Windows, forward slash / is replaced with backslash \\. + + Note: os.path.normpath also performs separator normalization, but it also + eliminates leading ./ which you may want to keep in your string. + ''' + if os.name == 'nt': + path = path.replace('/', '\\') + + # On unix, backslashes are valid filename characters so we do not normalize + # them to forward slash. + + return path + def system_root(): return Path(os.sep)