Python Forum
subprocess.call change color of shell - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: subprocess.call change color of shell (/thread-1632.html)



subprocess.call change color of shell - Aquaplant - Jan-17-2017

Hello there,
I am using the package 'subprocess' to call some programs with parameter shell=True. Now I wish to change the color of the produced output. My system is Linux Ubuntu 15 with python3.5

Can someone help me?
My latest approach:
import subprocess

print("Some text in default color.")
subprocess.run(['echo -e "\033[32m"', 'ls -l', 'echo -e "\033[0m"'], shell=True)
print("Hopefully some text in default color, again.")
Produces:
Output:
Some text in default color. -e Hopefully some text in default color, again. <- this is now green Process finished with exit code 0
I want something like this:
Output:
Some text in default color. #from now on in green total 2088 -rw-rw-r-- 1 aq aq    1990 Sep 29 22:08 bibtexCheck.py -rw-rw-r-- 1 aq aq 1936333 Jan 10 10:23 argparse #from now on in default color Hopefully some text in default color, again.
Thanks a lot,
Aquaplant


RE: subprocess.call change color of shell - wavic - Jan-17-2017

Hello!

See colorama if fits your needs


RE: subprocess.call change color of shell - buran - Jan-17-2017

https://pypi.python.org/pypi/colorama

https://pypi.python.org/pypi/termcolor


RE: subprocess.call change color of shell - wavic - Jan-17-2017

Pygments will do too


RE: subprocess.call change color of shell - Aquaplant - Jan-19-2017

Thanks for your suggestions. I will try them.

Sorrry, I didn't find out how you edit posts Huh

colorama seems to do the same: Print an ANSI escape character sequence and therefore throws errors:

import subprocess

print("Some text in default color.")
subprocess.run(["ls -l"], shell=True)
print("Hopefully some text in default color, again.")
Runs smoothly.
However this:
import subprocess
from colorama import Fore

print("Some text in default color.")
subprocess.run([Fore.GREEN, "ls -l", Fore.RESET], shell=True) #or subprocess.run(["%sls -l%s" % (Fore.GREEN,Fore.RESET)], shell=True) OR subprocess.run(["%s; " % Fore.GREEN, "ls -l;" "%s" % Fore.RESET], shell=True)
print("Hopefully some text in default color, again.")
Throws an error:
Output:
Some text in default color. Hopefully some text in default color, again. ls -l: 1: ls -l: : not found
This sometimes works for the first rows (still terminating with errors):
subprocess.run(["%s; ls -l; %s" % (Fore.GREEN,Fore.RESET)], shell=True)
Output:
/bin/sh: 1: : not found /bin/sh: 1: : not found
Do I misuse the argument list or something? Could it be the encoding of the shell?

With termcolor it seems that you need to predefine the text's color (colored("some text")). I also want the output of my called command in green...

pygment seems not suitable for this, too. It uses HTML, HTML formatter and so on.

Aquaplant


RE: subprocess.call change color of shell - sparkz_alot - Jan-19-2017

I'm not near the linux machine at the moment, but tried on the windows 10:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess
import colorama as cr

print("Some text in default color.")

cr.init()    # Needed for Windows, supposedly ignored by linux
print(cr.Fore.GREEN)

subprocess.run("dir", shell=True)

print(cr.Style.RESET_ALL)
print("Hopefully some text in default color, again.")
Output:

Output:
C:\Python\scratch.py Some text in default color.    # Default color #    This is in Green  Volume in drive C is Acer  Volume Serial Number is 8CE3-7690  Directory of C:\Python\ 01/19/2017  09:54    <DIR>          . 01/19/2017  09:54    <DIR>          .. 01/19/2017  09:54               479 scratch.py                1 File(s)         479 bytes                2 Dir(s)  907,242,061,824 bytes free Hopefully some text in default color, again.    #Back to default color C:\Python\>



RE: subprocess.call change color of shell - wavic - Jan-19-2017

Hello! You have to call colorama.init(). 

from colorama import Fore, Style, init

init()

print('{red}This is colored text!{reset}'.format(red=Fore.RED, reset=Style.RESET_ALL)



RE: subprocess.call change color of shell - Aquaplant - Jan-20-2017

(Jan-19-2017, 03:07 PM)sparkz_alot Wrote: I'm not near the linux machine at the moment, but tried on the windows 10:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import subprocess
import colorama as cr

print("Some text in default color.")

cr.init()    # Needed for Windows, supposedly ignored by linux
print(cr.Fore.GREEN)

subprocess.run("dir", shell=True)

print(cr.Style.RESET_ALL)
print("Hopefully some text in default color, again.")
Output:

Output:
C:\Python\scratch.py Some text in default color.    # Default color #    This is in Green  Volume in drive C is Acer  Volume Serial Number is 8CE3-7690  Directory of C:\Python\ 01/19/2017  09:54    <DIR>          . 01/19/2017  09:54    <DIR>          .. 01/19/2017  09:54               479 scratch.py                1 File(s)         479 bytes                2 Dir(s)  907,242,061,824 bytes free Hopefully some text in default color, again.    #Back to default color C:\Python\>

Thanks a lot, this worked. I just wonder if this temporary change is global or limited to one / my console(s).


RE: subprocess.call change color of shell - sparkz_alot - Jan-20-2017

If I understand the question correctly, it only affects the terminal you used to run the script.