0

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]

Krunky
  • 1
  • 1
  • It's interpreting the backkslashes as escape characters I think. Change all the \ to \\ and try it again. – michjnich Jun 12 '20 at 08:00
  • You start a unicode sequence when you do `"\U..."`. Either double your backslashes `"C:\\Users\\BRS\\Desktop\\wattpad.txt"` or use a raw string `r"C:\Users\BRS\Desktop\wattpad.txt"`. – Matthias Jun 12 '20 at 08:01
  • Does this answer your question? [(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape](https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca) – Jenne Jun 12 '20 at 08:08

1 Answers1

0

\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"

Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50