Check if dirname exists

We only want to path normalize a string (and replace backslashes with
forward slashes if on Windows) if it's actually a path. But checking if
there's a path with that name doesn't work if the path is a substring of
an actual path. Additionally check whether the dirname points to
something on the file system. If it does, it's likely meant to be
interpreted as a path.
This commit is contained in:
Henrik G. Olsson
2025-10-13 16:51:05 -07:00
parent b22e4d2bc4
commit 98fe7c0d1d

View File

@@ -23,7 +23,9 @@ import sys
# this normalizes Windows paths to use backslashes, we have to replace them
# back to forward slashes.
def normalize_if_path(s):
if not os.path.exists(s):
# Check dirname for cases like a file named `%t.out.txt`
# There won't be a `%t` path, but we still want to match this path substring.
if not os.path.exists(s) and not os.path.exists(os.path.dirname(s)):
return s
if platform.system() == "Windows":
return os.path.abspath(s).replace('\\', '/')