Search code examples
python-3.xpandasdataframecsvexport-to-csv

Using pandas to save to a csv file with date time appended to the filename


I was running the code but am getting this error

OSError: [Errno 22] Invalid argument: 'filename 09-30-2021 16:45:17 PM.csv'

Is there anyway to work around this?

currentDateTime = datetime.now().strftime("%m-%d-%Y %H:%M:%S %p")
df.to_csv(f"filename {currentDateTime}.csv", index = False)


Solution

  • Your example works on Linux, but it doesn't work on Windows because of the restricted characters: < > : " / \ | ? *

    Please replace : with something else.