Home > Software design >  PANDAS - CONVERTED CSV FILE AND CHANGE THE FILENAME TO DATE YESTERDAY
PANDAS - CONVERTED CSV FILE AND CHANGE THE FILENAME TO DATE YESTERDAY

Time:11-11

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

CodePudding user response:

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.

CodePudding user response:

try this:

from datetime import datetime


date_ = datetime.now().date() - pd.Timedelta('1D')
filepath = f"myfile{date_.strftime('%Y%m%d')}.csv"
df.to_csv (filepath)
  • Related