Home > Net >  Why can't I comment this while loop code?
Why can't I comment this while loop code?

Time:11-29

I got curious why I can't comment this while loop.

I get error

 '''while True:
    ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 191-192: truncated \UXXXXXXXX escape:
'''while True:
    stop_threads = False
    startTime = datetime.now()
    print("Запуск "   str(countStarts))
    sub = Popen(
        r'C:\Users\RT\Desktop\marketMyGames.air\контроль cmd\ConsoleApp1\ConsoleApp1\bin\Debug\netcoreapp3.0'   "\\"   numEmulator   '.bat',
        stdout=PIPE, stderr=STDOUT, shell=True)
'''

CodePudding user response:

The problem is you're using triple-quoted strings to "comment out" code. (What you're actually doing is just adding a no-op string literal to your code.)

The \U in

\Users\RT\Des

is being interpreted to start an Unicode escape sequence, but the rest of it is gibberish in Python's eyes.

You have a couple of options:

  • use # to comment out code, like people do in Python (you can teach your IDE to do that for every line, no need to do it by hand)
  • add a r marker to the triple-quoted string to avoid those escapes, i.e. r'''...

CodePudding user response:

You have a \U sequence in your multiline string (not really the same as a comment, although ''' is sometimes used as a workaround for multiline comments), but the characters that follow it aren't a valid Unicode escape sequence.

An easy fix is to make it a raw string -- add a r before the ''' and the error goes away.

CodePudding user response:

You can not comment this by turning it into a string because you encode unicode characters there, see the Python documentation here. The critical characters are C:\Users. If you drop there the U it will work. However you can apart from turning your while loop into a string always comment it by turning the section into real comment lines with #, see https://www.python.org/dev/peps/pep-0008/#block-comments.

  • Related