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: I want something like this: 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 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: This sometimes works for the first rows (still terminating with errors):subprocess.run(["%s; ls -l; %s" % (Fore.GREEN,Fore.RESET)], shell=True) 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:
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: 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. |