I have a piece of code iterating though a number of files in an AWS S3 folder. I want to collect the last modified datetimestamps for all objects and be able to sort them in ascending or descending order. my code looks like this:
import boto3
s3 = boto3.resource('s3')
my_bucket = s3.Bucket('myS3bucket')
for my_bucket_object in my_bucket.objects.all():
if my_bucket_object.key.startswith('inbox/'):
changedate_dt = my_bucket_object.last_modified
changedate_str = sorted(changedate_dt, reverse=False)
print(changedate_str)
I am getting the error:
TypeError: 'datetime.datetime' object is not iterable
how can I solve this?
You need to be making a list of your datetime.datetime
values, rather than simply trying to sort each individual datetime.datetime
:
changedate_list = []
for my_bucket_object in my_bucket.objects.all():
if my_bucket_object.key.startswith('inbox/'):
changedate_list.append(my_bucket_object.last_modified)
You can then sort the list of datetimes and print it out (in isoformat if you like):
changedate_list.sort()
for changedate in changedate_list:
print changedate.isoformat()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.