Tag Archives: SyntaxError: unexpected character after line continuation character

[Solved] SyntaxError: unexpected character after line continuation character

Note: the above code looks OK on the surface, but an error is reported: syntax error: unexpected character after line continuation character.

The reason for the problem here is:

There is a space after the line break ‘\’ on the first line. When there is a space after the newline character, the program will report an error.

Solution:

Very simple, just delete the spaces after the newline character “\” and the problem is solved.

In other words, nothing else is allowed after the newline character. Include comments#

Python SyntaxError: unexpected character after line continuation character

Python_ Syntax error: unexpected character after line continuation character

Reason: the content of the written file is incorrect and should be processed as a string

>>> import os
>>> os.makedirs(time_year+"\\"+time_month+"\\"+time_day)#time_year、time_month、time_day
>>> os.chdir(time_year\time_month\time_day)#Wrong
  File "<stdin>", line 1
    os.chdir(time_year\time_month\time_day)
                                          ^
SyntaxError: unexpected character after line continuation character

This is OK: OS. Chdir (time)_ year+”\\”+time_ month+”\\”+time_ day)

Refer to the writing method of creating directory in the first line

>>> os.chdir(time_year+"\\"+time_month+"\\"+time_day)#Right
>>> with open(time_hours+".txt","w+") as fp:
...     fp.read()
...