preserving color ouput of a shell command via os.popen()

Brett Hoerner bretthoerner at
Wed Sep 27 04:58:29 CEST 2006

zeezlo at wrote:
> child = os.popen("ls --color=auto")
> output =
> results in an output string which is free of the escape sequences
> generated by ls.

Don't use --color=auto,  from the 'ls' man page:

With --color=auto,  color codes are output only if standard output is
connected to a terminal (tty).

'ls' doesn't think Python is a color terminal, so just use "--color",

>>> import os
>>> a = os.popen("ls --color /")
>>> f =
>>> f
>>> a = os.popen("ls --color=auto /")
>>> f =
>>> f

See the difference? :)

Brett Hoerner

More information about the Python-list mailing list