If the output is being produced by some library that you are using and you don't have control over it, you can re-direct the standard output to a file when the program starts. Then you can close and re-open the file periodically to prevent the files to grow forever. You can use a timestamp for the name of the file so it is always diferent. Something like:
import sys
import datetime
current_stdout = None
def reset_stdout():
global current_stdout
if current_stdout:
current_stdout.close()
sys.stdout = open(
"{}.log".format(
datetime.datetime.now().strftime('%Y%m%d%H%M%S')
),
"w")
current_stdout = sys.stdout
reset_stdout()
# Then call this every day.. week ..
reset_stdout()
If you prefer to reset the file depending on the size of it, you can monitor periodically its size with something like this:
if current_stdout.tell() > 1000000:
reset_stdout()
But if you do have control over the output that you are sending, I strongly recommend to use the logging library. It has a lot of flexibility on what you can do with the output. The messages sent to the log can be processed by objects called handlers. One of the handlers included in the library do what you want, is called the RotatingFileHandler. From the documentation:
class logging.handlers.RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)
"You can use the maxBytes and backupCount values to allow the file to
rollover at a predetermined size. When the size is about to be
exceeded, the file is closed and a new file is silently opened for
output. Rollover occurs whenever the current log file is nearly
maxBytes in length; if maxBytes is zero, rollover never occurs."
So you can do all the logging using something like this:
import logging
import logging.handlers
# Create a logger
log = logging.getLogger('example')
# Set the level of the logger. By doing this all the messages with a "level" of INFO or higher will be
# sent to the log
log.setLevel(logging.INFO)
# Create the handler and set 20 files to rotate and a maximum size of 1MB
handler = logging.handlers.RotatingFileHandler('log',maxBytes = 1000000,backupCount=20)
# Now attach the handler to the logger object
log.addHandler(handler)
# Now you can send your output like:
log.info('text text text text text text text text text')
log.info(....