I think I found the answer. But I want to know if there is a better way to do it.
def convert_to_unc_path(path: str) -> str:
# Check if already escaped UNC path (starts with \\\\?\\)
if path.startswith("\\\\\\\\?\\\\"):
return path
# Check if raw UNC path (starts with \\?\)
elif path.startswith("\\\\?\\"):
return path.replace("\\", "\\\\")
# Regular path, add UNC prefix and escape
else:
unc_path = "\\\\?\\" + path
return unc_path.replace("\\", "\\\\")