The r
prefix is used to indicate the you want the string to be evaluated as "raw", keeping backslashes as-is.
Try this:
path = r"D:\final"
nameFile = "Res"
result = path + '\\' + nameFile + 'mat'
As you can see, I added r
before the string expression that contains a non-escaped backslash.
To see the difference, try doing :
print("\\")
print(r"\\")
(Without the parentheses if you're using Python2)
Also, I recommend using the pathlib
module of the standard library to handle paths properly. This will also help a lot if you try to make your code portable:
from pathlib import Path
(Path("D:/final") / path / nameFile).with_suffix('.mat')