Python SyntaxError: EOL while scanning string literal,

Source: Internet
Author: User
Tags eol


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



Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.