简体   繁体   中英

python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error

i have written a python code in which i have already assigned variable 'emailentry' a string value. what i want to do now is add that string value in the last line of my text file known as 'wattpad.txt'

i have written this code

with open("C:\Users\BRS\Desktop\wattpad.txt", 'a') as outfile:
    outfile.write(emailentry /n)

and getting error

File "C:\Users\BRS\Desktop\wattpad acc maker.py", line 41
    with open("C:\Users\BRS\Desktop\wattpad.txt", 'a') as outfile:
              ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

pls help [Finished in 0.2s]

\U is the start of an escape sequence in string litterals, as are a few others like \u , \n ...

If you want to have litteral backslashes in your string, you can either:

  • Escape them:

    "C:\\Users\\BRS\\Desktop\\wattpad.txt"

  • or better, use a raw string by prefixing the string with r :

    r"C:\Users\BRS\Desktop\wattpad.txt"

  • or use forward slashes, even on Windows:

    "C:/Users/BRS/Desktop/wattpad.txt"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM