-1

This is my code, but the result is not what I want,

def read_message():

   quotes = open("C:\Users\Administrator\Desktop\movie_quotes.txt")
   read_file = quotes.read()
   print(read_file)
   quotes.close()

read_message()

The result shows that:

File "E:/Python/p1/send_message.py", line 4
quotes = open("C:\Users\Administrator\Desktop\movie_quotes.txt")
             ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

And I do not understand the exact meaning. My python version is 3.5.3.

DawnZhang
  • 55
  • 2
  • 6

3 Answers3

2

You need to use a raw string, double your slashes or use forward slashes instead:

r'C:\Users\Administrator\Desktop\movie_quotes.txt' 'C:\\Users\\Administrator\\Desktop\\movie_quotes.txt' 'C:/Users/Administrator/Desktop/movie_quotes.txt'

Alen Jacob
  • 170
  • 1
  • 9
1

You have a SyntaxError. That means the error is in your source code itself, not something that happens during its execution.

In your code, "\U" in "\Users" is interpreted as a start of a long Unicode literal. Double the backslashes or use a raw string literal to fix that as others suggested.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
0

You have to use double slashes, like so :

quotes = open("C:\\Users\\Administrator\\Desktop\\movie_quotes.txt"), 

OR forward slashes:

quotes = open("C:/Users/Administrator/Desktop/movie_quotes.txt")
Milan Velebit
  • 1,933
  • 2
  • 15
  • 32