0

I'm trying to insert backslash , which is going to be used as a path variable but when I do this:

i='02222022130300393.png'
path=r'C:\Users\kavin.r\Desktop\test\Level_Typical_Images\'+i

I'm getting:

SyntaxError: EOL while scanning string literal

When I tried like this:

i='02222022130300393.png'
path=r'C:\Users\kavin.r\Desktop\test\Level_Typical_Images\\'+i

I got

FileNotFoundError:No such file or directory: 'C:\\Users\\kavin.r\\Desktop\\test\\Level_Typical_Images\\\\02222022130300393.png'
martineau
  • 119,623
  • 25
  • 170
  • 301
kavin raja
  • 1
  • 1
  • 2
  • For this specific case, the simplest approach is to use string formatting - since that would be the preferred way to put the strings together *anyway*, and it happens to avoid trying to use a single backslash at the end of the string. That said, you do not need backslashes here at all in the first place. – Karl Knechtel Apr 08 '22 at 07:19
  • As an aside: the `[r]` tag is for a programming language named R, not for Python's raw strings. – Karl Knechtel Apr 08 '22 at 07:19
  • @KarlKnechtel it works for Python too – CuCaRot Apr 08 '22 at 07:23
  • 1
    You should use `path = os.path.join(r'C:\Users\kavin.r\Desktop\test\Level_Typical_Images', i)` to join the path components and avoid the issue completely. Another possibility is to use `path = pathlib.Path('C:/Users/kavin.r/Desktop/test/Level_Typical_Images') / i`. – martineau Apr 08 '22 at 07:49

0 Answers0