1

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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Andrew M
  • 119
  • 6

2 Answers2

3

Your filename is being treated as several subdirectories then a file, namely

InternetSpeetTest_11/
  24/
    21.csv

Therefore the "No such file or directory" error is telling you those intermediate directories do not exist. Instead switch to something that doesn't look like a directory such as

'InternetSpeedTest_11-24-21.csv'
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Thank you!! The answer was staring me in the face the whole time! The proper change to the datetime input: you can get rid of the / by using %d%m%y rather than %D – Andrew M Nov 24 '21 at 17:28
  • Additional note: While on Windows `/` doesn't separate directories, it is still [explicitly forbidden in file names](https://stackoverflow.com/questions/10708334/how-can-i-create-files-on-windows-with-embedded-slashes-using-python) (and many languages/APIs will thus accept `/` as a directory separator, even on Windows). – Joachim Sauer Feb 09 '23 at 14:55
0

Regardless of operating system, / will be treated as a path separator, and cannot be part of the file name. Attempting to use a name like InternetSpeedTest_11/24/21.csv will actually try to use the name 21.csv inside the subfolder 24 of the folder InternetSpeedTest_11. This causes an error - not just wrong results - because opening a file for writing will not create intermediate folders.

The standard way to include dates in file names is to use ISO 8601 format: YYYY-MM-DD. Putting the year first, and using zero-padded numbers for the month and day, ensures that file names that vary only in the date will sort chronologically.

Python's datetime, date and time classes all provide a isoformat method which can be used to convert, without needing to remember any formatting codes. Thus:

from os import exists
from datetime.datetime import now

data = "example data,123.456"

filename = f'InternetSpeedTest_{now.date().isoformat()}.csv'
if exists(filename):
    with open(filename, 'a') as f:
        f.write(data)

This will append the data to a file named like InternetSpeedTest_2021-24-11.csv, creating it first if it doesn't exist.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153