0

I am running a command line tool that returns a coloured output (similar to ls --color). I run this tool via subprocess:

process = subprocess.run(['ls --color'], shell=True, stdout=subprocess.PIPE)
process.stdout.decode()

But the result is, of course, with the color instructions like \x1b[m\x1b[m which makes further processing of the output impossible.

How can I remove the colouring and use pure text?

Michael Dorner
  • 17,587
  • 13
  • 87
  • 117

2 Answers2

1

You example worked for me. You can also open the file with just 'w' and specify the encoding:

import subprocess

with open('output.txt', mode='w', encoding='utf-8') as file:
    process = subprocess.run(['ls', '-l'], stdout=file)

ukBaz
  • 6,985
  • 2
  • 8
  • 31
  • Thanks. What OS do you use? Is it maybe a macOS problem? – Michael Dorner Feb 03 '23 at 19:27
  • My tests were on Linux. What response do you get if you do `import sys; sys.getdefaultencoding()`? – ukBaz Feb 03 '23 at 20:53
  • I rephrased the question: It turns out the color output is the problem! – Michael Dorner Feb 04 '23 at 10:20
  • 1
    I am unable to reproduce the issue even if I add the `--color` option. Linux shows the color in the terminal but seems to be removing them when pass it to Python subprocess pipe. Is it really just `ls` that you are doing? There are Python commands to do that without subprocess. If you are doing something else which has ASCII control characters in the output, I've found this answer: https://stackoverflow.com/a/53965386/7721752 which might be helpful. – ukBaz Feb 04 '23 at 12:34
1

This works on my win10 and python 3.11 machine. Your command runs without any issue: In my IDE I can't see color, but this command works also subprocess.run(["ls", "--color=none"], shell=True, stdout=subprocess.PIPE).

Valid arguments are on my machine:

  • 'never', 'no', 'none'
  • 'auto', 'tty', 'if-tty'
  • 'always', 'yes', 'force'

Code:

import subprocess

process = subprocess.run(["ls", "-la"], shell=True, stdout=subprocess.PIPE)

with open("dirList.txt", 'w') as f:
    f.write(process.stdout.decode())

Output: total 14432

drwxr-xr-x 1 Hermann Hermann       0 Nov 19 08:00 .
drwxr-xr-x 1 Hermann Hermann       0 Aug 28  2021 ..
-rw-r--r-- 1 Hermann Hermann       0 Jan 28 09:25 .txt
-rw-r--r-- 1 Hermann Hermann    1225 Jan  6 00:51 00_Template_Program.py
-rw-r--r-- 1 Hermann Hermann     490 Jan 15 23:33 8859_1.py
-rw-r--r-- 1 Hermann Hermann     102 Jan 15 23:27 8859_1.xml
Hermann12
  • 1,709
  • 2
  • 5
  • 14