I am trying to iterate over dict, which contains dates as keys:
for d, dir in subdirs:
print("d=", d, ", dir=", dir)
where subdirs is
datetime.date(2016, 9, 29): ['tiles/37/U/DB/2016/9/29/0/'],datetime.date(2017, 2, 23): ['tiles/37/U/DB/2017/2/23/0/'], datetime.date(2016, 4, 5): ['tiles/37/U/DB/2016/4/5/0/']
and so on.
I am receiving
TypeError: 'datetime.date' object is not iterable
Why and how to fix?
Try this -
for d, dir in subdirs.items():
print("d=", d, ", dir=", dir)
If subdirs is a dict, using it in for will give you only the keys(in your case datetime.date object) and d, dir will try to unpack them. That is why the error.
Using the items() will return the (key, value) pair which will be unpacked and stored in d and dirs respectively
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With