MMarie
MMarie

Reputation: 35

Cannot append date to a filename

Good morning,

Why I cannot append date to a filename when saving a dataframe to excel? Here is my code.

date = '24/12/2020'
datetime_object = datetime.strptime(date, '%m/%d/%Y')

writer = pd.ExcelWriter('Weekly_Report_'+date+'.xlsx', engine='xlsxwriter')
df.to_excel(writer,sheet_name='Scheduled Report', index=False)
writer.save()

The error is:

FileNotFoundError: [Errno 2] No such file or directory: 'Weekly_Report_24/12/2020.xlsx'

Upvotes: 0

Views: 37

Answers (1)

Sanchit Agarwal
Sanchit Agarwal

Reputation: 156

You are attempting to include forward slashes in the file name which is prohibited. Use some other character such as '-' as a separator in the date (e.g. 24-12-2020).

Read more - Is it possible to use “/” in a filename?

Upvotes: 2

Related Questions