I have a script that I will run repeatedly. I want it to log results to output files, using a different file each day. I decided to use the date in the file name, like so:
from os import exists
from datetime.datetime import now
data = "example data,123.456"
filename = 'InternetSpeedTest_' + now.strftime("%D") + '.csv'
if exists(filename):
with open(filename, 'a+') as f:
f.seek(0, 0)
a = f.read()
f.write('\n')
f.write(data)
else:
with open(filename, 'w+') as f:
f.write(data)
But this gives me an error when it tries to open the file (either to start a new one or to append):
FileNotFoundError: [Errno 2] No such file or directory: 'InternetSpeedTest_11/24/21.csv'
I've also tried specifying an absolute path to the file, but the problem remains.
Why does this happen, and how can I fix it?