Bootstrap FreeKB - Python (Scripting) - Escape backslash
Python (Scripting) - Escape backslash

Updated:   |  Python (Scripting) articles

Here is an example of how you can escape/replace backslashes.

#!/usr/bin/python3
import re
directory = "C:\Users\john.doe"
directory = re.sub("\\", "/", directory)
print(f"directory = {directory}")

 

Or sometimes you may need to use 4 backslashes.

#!/usr/bin/python3
import re
directory = "C:\Users\john.doe"
directory = re.sub("\\\\", "/", directory)
print(f"directory = {directory}")

 

Better yet, Python3 r formatter can be used.

#!/usr/bin/python3
directory = r"C:\Users\john.doe"
print(f"directory = {directory}")

 

Which should return the following.

directory = C:/Users/john.doe

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 4945a7 in the box below so that we can be sure you are a human.