Kyle Sponable
Kyle Sponable

Reputation: 735

How to save time data to a variable in python

this code works but I cant save the first datetime entry

import csv
import sys
import datetime

f = open(sys.argv[1], 'rt')

now = datetime.datetime.now()
timeStart = str(now)


try:
    reader = csv.reader(f)
    for row in reader:
        #Do stuff 
    timeStop = str(now)
    print str(timeStart)
    print timeStop
    print count

finally:
    f.close()

The problem is TimeStart and TimeStop agree so I cant really test how long the program has been running advise?

Upvotes: 0

Views: 12094

Answers (2)

Simon Fraser
Simon Fraser

Reputation: 2818

now = datetime.datetime.now() stores the current time in a variable called now. Later on, you're not asking datetime "What is the current time?", you're looking at the variable you stored the time in earlier on.

It's like looking at the clock and writing down that you started work at 9am, and then when you leave work, looking at the bit of paper, not the clock.

time_stop = datetime.datetime.now()

Upvotes: 3

Denziloe
Denziloe

Reputation: 8131

At now = datetime.datetime.now() you pass the time at that point in time into the now variable, where it is stored. It's a constant. It won't change the next time you print now. If you want it to update you have to get the current time again. So you need timeStop = str(datetime.datetime.now()).

Upvotes: 1

Related Questions