acturtle / cashflower

An open-source Python framework for actuarial cash flow models
https://cashflower.acturtle.com
MIT License
38 stars 9 forks source link

Logging with redirection #366

Open zchmielewska opened 6 months ago

zchmielewska commented 6 months ago

You can redirect prints from terminal to the external text file.

Something like that:

import os
import sys

# Open a file
file = open("output.txt", "w")

# Get the file descriptor
fd = file.fileno()

# Duplicate the file descriptor to the standard output
os.dup2(fd, sys.stdout.fileno())

# Now any print statement will write to the file
print("This message will be written to the file")

# Close the file
file.close()

Perhaps it can be useful for debugging.