Nov-28-2022, 11:21 AM
Dear rob101, thank you for the suggestion. First, I would have to check, whether slicing makes sense, i.e, Is there a double backslash at the end of the string? The input of the directory may come from the user and it cannot be predected.
I can replace using a regex
I can replace using a regex
print(re.sub(r"(?<=[\\])(\\)", r"", r"dir1\dir2\\dir3\\\dir4\\\\"))and end up with an expected:
Output:dir1\dir2\dir3\dir4\
I understand that it is a cosmetic issue. The operating system is going to collapse multiple backslashes into a single one, or ignore the superfluous. The script works anyway.