I would like to convert a string temp.filename.txt to temp\.filename\.txt using python
Tried string replace method but the output is not as expected
filename = "temp.filename.txt"
filename.replace(".", "\.")
output: 'temp\\.filename\\.txt'
filename = "temp.filename.txt"
result=filename.replace(".", "\.")
print(result)
I stored a result in variable(result) its working fine check this
replacemodifies the string you call it on. Did you meanfilename=filename.replace(".", "\.")?\.isn't recognized as a special escape sequence. You should always double up \\ when using it in a string:filename.replace(".", "\\.").r""strings, but even these can trick you, e.g. if your string finishes with a \, you'll still need to double up that last \...