Uncategorized

Create current and most recent datetime file – python


I’m trying to create a log file using python. The specific problem I’m trying to solve is separating datetime into current and previous timestamps based on when the script was executed.

Using below, I want to generate a df that contains two cols (DateTime, Last DateTime), where DateTime returns the datetime of when the script is executed and Last DateTime returns the most recent or previous time datetime the script was executed.

I want to return these timestamps to a df called execution_file.

import datetime
import pandas as pd

times = pd.DataFrame(columns=['Last Datetime', 'DateTime'])

ct = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

def get_current_run_time_stamp(df):

    # try loading the datetime of the last run, else print warning
    try:
        df['DateTime'] = ct
    except:
        # Return current time-stamp if no time exists 
        return datetime.datetime.now().strftime(ct)

execution_file = get_current_run_time_stamp(times)

print(execution_file)

expected output:

if script is initially executed at 2000/01/30 14:00:00 and then again 30 minutes later, I’m hoping to df will read:

         Last Datetime             Datetime
0                  NaN  2000/01/30 14:00:00
1  2000/01/30 14:00:00  2000/01/30 14:30:00



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *