[ACCEPTED]-Python - issues with numbers in pathnames-python

Accepted answer
Score: 15

The backslash is special in C-style strings 15 like Python uses, just like in C++, C#, and 14 Java. Either use a double-backslash to 13 say “yes, I really mean a backslash,” not 12 the character code \201, or use an r'' string that 11 does not interpret backslash sequences:

'D:\\Folder\\2010\\file.pdf'
r'D:\Folder\2010\file.pdf'

Note 10 that this issue does NOT come up with variables! Once 9 you create a string correctly, it always 8 keeps its value; it does NOT get re-interpreted, and 7 have backslashes cause problems all over 6 again, each time you pass the value to a 5 function, so open(myvar) should see exactly the same 4 string you get when you print(myvar).

(I think that on 3 Windows you may also be able to just use 2 forward slashes, which require no special 1 quoting:)

'D:/Folder/2010/file.pdf'
Score: 0

Python automatically converts forward slashes 3 to back slashes in Windows pathnames (This 2 is because other OSes that Python runs on, including 1 Linux and Mac, use forward slashes natively).

More Related questions