-5

This has been asked before, but none of the solutions I've found have worked for me.

I am trying to do this tutorial from w3schools. I am trying to open a .py file I created in a text editor https://www.w3schools.com/python/python_getstarted.asp

I get the below

>>> C:\Users\Acer ES 15\Desktop\demo_string_input.py
  File "<stdin>", line 1
    C:\Users\Acer ES 15\Desktop\demo_string_input.py
                                                   ^
SyntaxError: unexpected character after line continuation character

I think the problem has something to with the \U. There have been many suggestions that don't work including putting quotes around the filepath, putting an 'r' in front, duplicating and triplicating the backslashes. None of it has worked.

The problem may also have something to do with the space in my username (Acer ES 15). Putting quotes aound the filepath gives the below

>>> 'C:\Users\Acer ES 15\Desktop\demo_string_input.py'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in 
position 2-3: truncated \UXXXXXXXX escape
>>>

I am using Windows 10 and the Python 3.7 interface available at https://www.python.org/

I am getting the file path by right clicking the file and clicking 'copy full filepath'

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Frankie
  • 11
  • 1
  • 3
  • 1
    `C:\Users\Acer ES 15\Desktop\demo_string_input.py` is not vaiid Python syntax. You can't paste a script filename into the interactive interpreter, use the command line instead to run `python C:\Users\Acer ES 15\Desktop\demo_string_input.py`. – Martijn Pieters Sep 24 '18 at 16:49
  • (But with quotes around the file path, because you've picked a user name with spaces in it.) – user2357112 Sep 24 '18 at 16:51
  • Thank you both for the help but it has not worked. I have tried various commands based on your suggestions. I'll paste the errors below. `>>> "C:\Users\Acer ES 15\Desktop\hello.py"` File "", line 1 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape >>> `>>> python C:\Users\Acer ES 15\Desktop\demo_string_input.py` File "", line 1 python C:\Users\Acer ES 15\Desktop\demo_string_input.py ^ SyntaxError: invalid syntax >>> – Frankie Sep 24 '18 at 17:16
  • from python command line `>>> run C:\Users\Acer ES 15\Desktop\demo_string_input.py` – Matt Sep 24 '18 at 17:29

1 Answers1

0

When you use \ in a string in python, it means that the next character may have special significance. For example, \r and \n refer to carriage returns and line feeds, respectively.

When you specify \U it indicates that you're providing a unicode character. The interpreter attempts to read the next four to eight characters to attempt to figure out which single character you actually want. This is critically important for multi-byte characters.

If you want to actually use a \ in a string, you have two choices. You can either use \\, or you can use a raw string r'\'. The lower case r tells the interpreter not to treat \ as the beginning of an escape sequence.

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111