How to change filename of csv file to yesterday's date
Question:
Currently I’m adding a feature on my python script that can rename the converted file with yesterday date format right now my script is working with current date my plan is to adjust it to yesterday.
can someone help me what I need to modify with my script I already test some script but still not working 🙁
my current script (get current date)
df.to_csv (f"myfile{pd.datetime.datetime.now().date().strftime('%Y%m%d')}.csv")
Thank you in advance guys
Answers:
You can subtract a day. Use the datetime
module to make it a bit easier.
import datetime as dt
f"myfile{(dt.date.today()-dt.timedelta(days=1)).strftime('%Y%m%d')}.csv"
You may want to use dt.datetime.utcnow()
, depending on how you deal with time zones.
Currently I’m adding a feature on my python script that can rename the converted file with yesterday date format right now my script is working with current date my plan is to adjust it to yesterday.
can someone help me what I need to modify with my script I already test some script but still not working 🙁
my current script (get current date)
df.to_csv (f"myfile{pd.datetime.datetime.now().date().strftime('%Y%m%d')}.csv")
Thank you in advance guys
You can subtract a day. Use the datetime
module to make it a bit easier.
import datetime as dt
f"myfile{(dt.date.today()-dt.timedelta(days=1)).strftime('%Y%m%d')}.csv"
You may want to use dt.datetime.utcnow()
, depending on how you deal with time zones.