Want to convert below path from single slash to double slash but when i print file its removing \b and \U as its special char in python. And for \U its gives unicode error.
file = "C:\Users\Dell\Desktop\ProjectShadow\button\button.py"
Expected output :
file = "C:\\Users\\Dell\\Desktop\\ProjectShadow\\button\\button.py"
CodePudding user response:
Use the r
for raw string and replace all single slashes with double slashes using the str.replace
method.
file = r"C:\Users\Dell\Desktop\ProjectShadow\button\button.py"
file = file.replace("\\", "\\\\")
C:\\Users\\Dell\\Desktop\\ProjectShadow\\button\\button.py
CodePudding user response:
path = "C:\U0005\b000"
# replace single slash with double slash
double_slash_path = path.replace("\\", "\\\\")
# escape special characters
double_slash_path = double_slash_path.replace("\b", "\\b")
double_slash_path = double_slash_path.replace("\U", "\\U")
print(double_slash_path)