Posts

Showing posts from June, 2018

Python - datetime Formatting

Formatting datetime in Python can be a 'pain'. Formatting works in two directions - From a string to datetime. - From a datetime to string. The easiest way to remember how things work using the strptime and strftime is that: strptime (string)(parse)(time) strftime (string)(format)(time) Format a datetime to a string from datetime import datetime print(datetime.today( ).strftime('%Y-%m-%d %H:%M:%S')) 2020-07-22 10:07:16 Parse a String to datetime from datetime import datetime date_time = ' 2020-07-22 10:07:16 ' dt_object =  datetime .strptime(date_time, ' %Y-%m-%d %H:%M:%S' )

Rename Key in S3 (Python)

S3 has no rename functionality. You need to copy to a new 'file' and delete the original. In python using boto3 s3 = boto3 . resource ( 's3' ) s3 . Object ( 'my_bucket' , 'my_file_new' ). copy_from ( CopySource = 'my_bucket/my_file_old' ) s3 . Object ( 'my_bucket' , 'my_file_old' ). delete ()