pythonwindowscmdansi-escape

ANSI escape color doesn't work in windows 10 cmd.exe


This is my python code.

print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m')

I'm trying to see a green background color on the cmd.exe. It's strange. It works properly when I execute it with command 'start /B python x.py'. But it doesn't work when I execute it with command just 'python x.py'

You can see it from following picture. Could you please give some advice for this?

enter image description here


Solution

  • When you run your script with start /B python x.py the start command is creating a new console window with different settings that happen to support ANSI colors. Windows command prompt doesn't process ANSI escape sequences unless they're explicitly enabled.

    Try this

    import os
    os.system('')  
    print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m')
    

    The output

    enter image description here