Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

Here's the code:

'''

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)

1 Answer

0 votes
by (36.8k points)

The problem is with a file_path variable between triple quotes. C:\ starts an eight-character Unicode escape. In the code, the escape is followed by the character U, which is invalid.

Double up your backslashes to escape them, e.g.:

"""

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"

Do check out Data Science with Python course which helps you understand from scratch 

Browse Categories

...