0

When trying to read text from a file where it contains ANSI escape color codes it just prints the color codes and does not show colors

This was both tested in Windows 10 and MSYS

Text File(title.txt):\033[0;32m Example \033[0m

Code:

def ShowText():
    ## Declare the path of the title
    titlePath = r'display\title.txt'

    ## Try to read the title from the given text
    try:
        ## Open the file in read mode
        f = io.open(titlePath, mode="r", encoding="ascii")
    
        ## Print the read text
        print(f.read())

    except (IOError, OSError) as e:
        print("Could not read text")

Text Output: \033[0;32m Example \033[0m

Print: [1]: https://i.stack.imgur.com/se4MS.png

I also tried importing and initializing colorama and use termcolor cprint but the results are equal

Miguel Silva
  • 65
  • 2
  • 8
  • The test can be reduced to `python3 -c "print('\033[0;32m Example \033[0m')"` which works fine under Linux. The problem is with Windows console. See [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/q/16755142/95735). – Piotr Dobrogost Sep 29 '20 at 20:38
  • You need to [enable virtual terminal processing](https://learn.microsoft.com/en-us/windows/console/setconsolemode). – IInspectable Sep 29 '20 at 20:45
  • It could also of unescaping the string with: f.read().decode('unicode_escape') – Sebastian Peterlin Sep 29 '20 at 20:46

0 Answers0