My interpreter suggests that you are mistaken in your belief that the second example does not work, because
>>> r"D:\final\Res.mat" == "D:\\final\\Res.mat"
True
The correct way to build file paths from components is by using the os.path.join
function, which can take multiple arguments and is portable across platforms. I would suggest you try something like
result = os.path.join(path, nameFile+".mat")