Python SyntaxError: EOL while scanning string literal,
The error is because the string ends with \ or the string lacks quotation marks.
This error occurs when writing code and splicing windows paths. Only by checking the information can you find that strings in python cannot end.
My code is as follows:
Import osdirname = "test"
Path = r'c: \ Users \ panda \ Desktop \ New Folder \ '+ dirname
An error is reported when running
File "test. py", line 3 path = r'c: \ Users \ panda \ Desktop \ New Folder \ '+ dirname ^ SyntaxError: EOL while scanning string literal
How can this problem be solved?
Method 1: Use OS. path. join
Path = OS. path. join (r'c: \ Users \ panda \ Desktop \ NEW FOLDER ', dirname)
Method 2: escape the path backslash instead of r
Path = 'C: \ Users \ panda \ Desktop \ New Folder \ '+ dirname
Why cannot a string end with a \ (backslash )?
This is because the backslash is useful. When a complete string in python is too long, you can use a backslash to wrap a line when writing a line but maintaining it as a string, therefore, the backslash cannot be immediately followed by the string ending quotation marks.
References:
SyntaxError: EOL while scanning string literal solution-CSDN blog
Python: SyntaxError: EOL while scanning string literal-Stack Overflow