Python Forum
subprocess.call change color of shell
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess.call change color of shell
#1
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
Reply
#2
Hello!

See colorama if fits your needs
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
https://pypi.python.org/pypi/colorama

https://pypi.python.org/pypi/termcolor
Reply
#4
Pygments will do too
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
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
Reply
#6
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\>
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#7
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)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(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).
Reply
#9
If I understand the question correctly, it only affects the terminal you used to run the script.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 990 Oct-25-2023, 09:09 AM
Last Post: codelab
  simplekml change shape&color issac_n 2 2,828 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  continue if 'subprocess.call' failes tester_V 11 5,154 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing out the contents aftre subprocess.call() Rakshan 3 2,748 Jul-30-2021, 08:27 AM
Last Post: DeaD_EyE
  subprocess call cannot find the file specified RRR 6 16,563 Oct-15-2020, 11:29 AM
Last Post: RRR
  Change the color automatically Dragonos 5 2,838 Jul-28-2020, 01:17 PM
Last Post: Dragonos
  Use of input function to change screen background color in Turtles Oldman45 3 4,869 Jul-10-2020, 09:54 AM
Last Post: Oldman45
  Why wont subprocess call work? steve_shambles 3 2,670 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  subprocess.call - Help ! sniper6 0 1,517 Nov-27-2019, 07:42 PM
Last Post: sniper6
  How to use subprocess send commands to windows shell hlhp 3 4,433 Nov-26-2019, 04:40 AM
Last Post: LeanbridgeTech

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020