Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: an integer is required python

This is my code:

today = datetime.date.today()

if len(sys.argv) > 1:
    arg_month = sys.argv[1]
    arg_year = sys.argv[2]
    print arg_month
    print arg_year
    lastMonth = datetime.date(day=1, month=arg_month, year=arg_year)
    first = lastMonth + datetime.timedelta(month=1)
    lastMonth = lastMonth.strftime("%Y%m")
    curMonth = first.strftime("%Y%m")   
else:
    first = datetime.date(day=1, month=today.month, year=today.year)
    lastMonth = first - datetime.timedelta(days=1)
    lastMonth = lastMonth.strftime("%Y%m")
    curMonth=(time.strftime("%Y%m"))

This is how I run the code: python lelan.py 01 2015

the output is:

01
2015
Traceback (most recent call last):
  File "lelan.py", line 22, in <module>
    lastMonth = datetime.date(day=1, month=arg_month, year=arg_year)
TypeError: an integer is required

How to fix this? Thank you.

like image 403
Lelan Calusay Avatar asked Apr 14 '26 02:04

Lelan Calusay


1 Answers

It's because arguments from sys.argv are strings. You need to cast them to integers:

arg_month = int(sys.argv[1])
arg_year = int(sys.argv[2])
like image 159
luk32 Avatar answered Apr 15 '26 15:04

luk32



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!