[help] How can I write my output into a file while keeping color
See original GitHub issueIs it possible to write our output into a file while keeping the color. And when we do less -R
see it with color?
I have tried following:
from rich.console import Console
from datetime import datetime
with open("report.txt", "wt") as report_file:
console = Console(file=report_file)
console.rule(f"Report Generated {datetime.now().ctime()}")
console.print("[blue]hello[/blue]")
console.file.write("[blue]hello[/blue]")
but hello
is written without color into the report.txt
file.
When I try console.file.write("[blue]hello[/blue]")
screenshot for cat report.txt | less -R
:

Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
How to write stdout to file with colors? - Stack Overflow
I'd like to know if there's a way (in Linux) to write the output to a file with colors. I'm trying to use...
Read more >How to Write Stdout to a File with Colors | Baeldung on Linux
In this tutorial, we'll cover some tools we can use to write standard output to files while preserving their colors.
Read more >Maintain color from script output for later viewing
You can use the less -R <filename>.<fileformat> command to open your output file ...
Read more >How to write Stdout to a file with colors? - Tutorialspoint
To create some coloured output, we'll use the echo command; to store the output, we'll pipe it with the tee command. $ echo...
Read more >Saving output of a grep into a file with colors - Ask Ubuntu
Now, when you print the file to the console it will be printed with the colors, because Bash interprets those characters as "use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please see https://rich.readthedocs.io/en/latest/console.html#terminal-detection
Did I solve your problem?
Consider sponsoring the ongoing work on Rich and Textual.
Or buy me a coffee to say thanks.
– Will McGugan