0

I am trying to open a text file 'Person.txt' and I get this error

This is my code:

PD = open('C:\Users\Scooter\PeopleList.txt')

This is the file:

a
b
c
d

This is the error:

C:\Users\Scooter\PycharmProjects\class.py\venv\Scripts\python.exe C:/Users/Scooter/PycharmProjects/class.py/main.py
  File "C:\Users\Scooter\PycharmProjects\class.py\main.py", line 1
    PD = open('C:\Users\Scooter\PeopleList.txt')
                                               ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Aaron
  • 15
  • 8

1 Answers1

0

Backslash \ is the escape character, to use a literal backslash in a string you must escape it by using \\

PD = open('C:\\Users\\Scooter\\PeopleList.txt')

Or use a raw string to "disable" this escaping behaviour by prefixing the string with r

PD = open(r'C:\Users\Scooter\PeopleList.txt')
Iain Shelvington
  • 31,030
  • 3
  • 31
  • 50