[英]Why I am having syntax error when I am using multilines quote?
這是代碼:
'''
file_path = (r'C:\Users\Luka\Desktop\Pyhton exercises\pi_digits.txt')
with open(file_path) as file_object:
contents = file_object.read()
print(contents)
#print(contents.rstrip()) # rstrip should remove blank space at the end
'''
filename = ("prova.txt")
with open(filename) as file_object:
for line in file_object:
print(line)
問題在於三引號之間的file_path
變量。 C:\
開始一個八字符的 Unicode 轉義。 在您的代碼中,轉義符后跟字符U
,這是無效的。
加倍你的反斜杠以逃避它們,例如:
"""
file_path = (r'C:\\Users\\Luka\\Desktop\\Pyhton exercises\\pi_digits.txt')
with open(file_path) as file_object: contents = file_object.read() print(contents) #print(contents.rstrip()) # rstrip should remove blank space at the end
"""
filename = "prova.txt"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.