How to rename a local file daily via code

Background:
I am in the process of working out the final details of a complete HVAC automation overhaul. While testing I was writing a custom log file using notify.file. That file is called hvaclog.csv. This has worked fine with the exception that notify.file always appends to the file. I would like to start fresh each day with a clean hvaclog.csv. My plan was to rename hvaclog.csv to hvaclog.bak right after midnight, then follow with a notify.file write to start a new hvaclog.csv, since the file no longer exists.

I’ve looked at the various other file commands available to HA, but none seem to offer anything more than monitoring of file and folder size. Next I attempted to use a python script. I already have a borrowed python script on my system, so the infrastructure works but I get an error about restricted globals. Here is the full error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/homeassistant/components/python_script/__init__.py", line 196, in execute
    exec(compiled.code, restricted_globals)
  File "purge_hvac_log.py", line 1, in <module>

And here is the complete ‘purge_hvac_log.py’ script:

import os
if os.path.exists("hvaclog1.csv"):
  os.rename(r'hvaclog1.csv',r'file hvaclog1.bak')
else:
  print("The file does not exist")

I suspect it is the import.os command that raises the error (line 1), but I’m not sure. Regardless, I am searching for a simple method to either rename or (less desirably) delete the log file each day.

My Home Assistant install is running on a Mac desktop, simple install, no VENV, no Docker, just the only thing running on that machine. Any suggestions on how I can accomplish this (using ANY method) would be greatly appreciated.

The canonical tool for rotating log files is, funnily enough, logrotate. Install it with homebrew, configure it to rotate your log file (and purge old logs), and call it daily with cron.