Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syntaxerror: unexpected character after line continuation character in python

Tags:

python

file

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(" ");
like image 950
user642564 Avatar asked Dec 10 '22 09:12

user642564


2 Answers

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.

like image 61
unwind Avatar answered Feb 02 '23 01:02

unwind


Replace

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

by

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

  1. File path needs to be a string (constant)
  2. need colon in Windows file path
  3. space after comma for better style
  4. ; after statement is allowed but fugly.

What tutorial are you using?

like image 24
John Machin Avatar answered Feb 01 '23 23:02

John Machin