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

zeezlo at yahoo.com zeezlo at yahoo.com
Tue Sep 26 22:07:54 EDT 2006


Hi everyone

I would like to get the output of a shell process on Linux (eg, "ls
--color=auto") run via os.popen(), filter it and then output parts of
it while preserving any original coloring added by that process.

Doing

child = os.popen("ls --color=auto")
output = child.read()

results in an output string which is free of the escape sequences
generated by ls.

Is there a way to preserve them when I capture whatever the processes
writes to stdout? I could add them manually (or use curses), but then I
would just be duplicating the work done by ls.

Thanks in advance

Juergen




More information about the Python-list mailing list