Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
750 views
in Technique[技术] by (71.8m points)

file - syntaxerror: unexpected character after line continuation character in python

Can anybody tell me what is wrong in this program? I face

syntaxerror unexpected character after line continuation character

when I run this program:

f = open(D\python\HW\2_1 - Copy.cp,"r");  
lines = f.readlines();

for i in lines:  
    thisline = i.split(" ");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to quote that filename:

f = open("D\python\HW\2_1 - Copy.cp", "r")

Otherwise the bare backslash after the D is interpreted as a line-continuation character, and should be followed by a newline. This is used to extend long expressions over multiple lines, for readability:

print "This is a long",
      "line of text",
      "that I'm printing."

Also, you shouldn't have semicolons (;) at the end of your statements in Python.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...