0

I am trying to make file name before saving dataframe as csv. Here is my code:

import glob
filename = 'Top20Apps' + ' ' +pd.to_datetime("now").strftime("%Y/%m/%d")+'.csv'

Then I am trying to check whether the any csv file with same name exists or not by following code:

files_present = glob.glob(filename)
    if files_present:
        df.to_csv(filename,index=False,mode='a',header=False)
    else:
        df.to_csv(filename,index=False)

When I ran the above code, it throws the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'Top20Apps 2021/02/01.csv'

Error is showing on the else section. I am not sure where I made the mistake. Any help to fix the issue would be highly appreciated.

1 Answer 1

1

I think you need change .strftime("%Y/%m/%d") to .strftime("%Y_%m_%d"), because / is not valid value for filename under Windows.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.