How to preserve colors when capturing output from Git commands?
问题 I have a custom Git command implemented in Python that uses methods from the subprocess module to call git . I've noted that, depending on the method used, the output may or may not be colored, e.g.: import subprocess subprocess.run('git push origin --delete foobar', shell=True) print() print(subprocess.run('git push origin --delete foobar', shell=True, capture_output=True, encoding='utf-8').stderr, end='') Output: How to preserve colors in the captured output (both stdout and stderr)? 回答1: