Error executing script: strftime() takes at most1 argument(2given)

Asked 2 years ago, Updated 2 years ago, 43 views

Here's the script:

import datetime
from dateutil.relativedelta import relativedelta
importos

today = datetime.datetime.today()
one_month=today-relatedelta(months=1)
# print("one_month_ago->" + datetime.strftime(one_month_ago, '%Y-%m')))
now=datetime.datetime.now()

def down (dtime):
    i=dtime.strftime(one_month, 'chat-%Y%m.csv')
    print(i)

down (now)

Here's the error.

Traceback (most recent call last):
  File "test3.py", line 14, in <module>
    down (now)
  File "test3.py", line 11, in down
    i=dtime.strftime(one_month, 'chat-%Y%m.csv')
TypeError: strftime() takes at most 1 argument (2given)

That's all, but I don't know how to change the above two arguments, although I didn't know how to change them.
Thank you for your cooperation.

python python3

2022-09-30 21:48

2 Answers

Rather than the error itself, it seems that you would like to retrieve the year and year before one month, so the following article will be helpful.

Calculate the next day or month in Python

However, I don't know what and when I want to do it, so it's hard to give advice.
You create a function called down and specify the dtime parameter, but you use one_month without any specific explanation.

dtime.strftime(one_month, 'chat-%Y%m.csv') alone, you can do one of the following:

dtimeString using the parameter No.1 (corresponding only to error messages)

i=dtime.strftime('chat-%Y%m.csv')

dtimeString using parameters No. 2 (how to specify two parameters)

i=datetime.datetime.strftime(dtime, 'chat-%Y%m.csv')

String using one_month

i=datetime.datetime.strftime(one_month, 'chat-%Y%m.csv')

And if you want to string a month before the dtime parameter, here it is.

one_month_ago=dtime-relatedelta(months=1)
i=datetime.datetime.strftime(one_month_ago, 'chat-%Y%m.csv')

Or if you want to put them together in one line, click here.

i=datetime.datetime.strftime((dtime-relativedelta(months=1)), 'chat-%Y%m.csv')


2022-09-30 21:48

The strftime method is a datetime class method with a date/time format string in the argument.

How to use strftime function in Python [for beginners] is easy to understand.


2022-09-30 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.