0
src = r'C:\Users\'

I keep getting an error when assigning the above string to a variable (SyntaxError: EOL while scanning string literal). It has something to do with the last backslash. I would have thought making it a raw string would prevent this. What is causing this?

Stidgeon
  • 2,673
  • 8
  • 20
  • 28
  • 1
    Possible duplicate of [What exactly do “u” and “r” string flags do, and what are raw string literals?](https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-and-what-are-raw-string-literals). From the accepted answer: in a raw string literal, a backslash is "taken as meaning 'just a backslash' (_except when it comes right before a quote that would otherwise terminate the literal_)". – Brian61354270 Apr 08 '20 at 22:27

1 Answers1

2

The documentation states

Even in a raw literal, quotes can be escaped with a backslash, but the backslash remains in the result; (...) Specifically, a raw literal cannot end in a single backslash (since the backslash would escape the following quote character).

Instead, you can just do src = 'C:\\Users\\'.

aeternalis1
  • 675
  • 4
  • 7