My script creates files to export by concatenating a file path with a file type suffix. For example,
int = 3
print r"C:\Test\\" + str(int) + ".jpg"
This returns C:\Test\\3.jpg
I thought that because I included the ‘r’ at the beginning, I wouldn’t need a double backslash at the end, because it’d be treated as a raw string. But the following doesn’t work:
int = 3
print r"C:\Test\" + str(int) + ".jpg"
…presumably because Python is seeing an escape character before the end quote. So what’s the point of the ‘r’ then? And while my files get saved in the right place when exported, I don’t like that the print command gives me two backslashes after Test (eg C:\Test\3.jpg) when it just gives me one after C:
How can I get a single backslash in my file paths please?
Don’t try and use string manipulation to build up file paths. Python has the
os.pathmodule for that:This will ensure the path is constructed properly.
(Also, don’t call an integer
int, as it shadows the built-inint()function.)