Find latest CSV File from S3 bucket using boto3, Python

I want to find latest .csv file from the S3 Bucket which contains multiple format file like .json, .xlsx, .csv, .txt files.

Client = boto3.client('s3', aws_access_key_id=S3_AccessKey, aws_secret_access_key=S3_SecretKey) Response = Client.list_objects_v2(Bucket=S3_BucketName, Prefix=PrefixPath)  Files_ListS = Response.get('Contents') 

Below script gives latest file from S3 and I am getting some .json file (bcz recently updated), I want to .csv file which is updated before json file, means latest from csv files.

max(Files_ListS , key=lambda x: x['LastModified']) 
Add Comment
1 Answer(s)

You can filter for only CSV files using a list comprehension by checking if the object keys end with .csv:

csv_objects = [f for f in Files_ListS if f['Key'].endswith('.csv')] max(csv_objects, key=lambda x: x['LastModified']) 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.