I am trying to create a timestamp variable which actually stores the present time and outputs the time at that instance. A reproducible code is also included:
import time
from datetime import datetime
t = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for i in range(5):
time.sleep(2)
print t
Output
2015-03-11 11:28:53
2015-03-11 11:28:53
2015-03-11 11:28:53
2015-03-11 11:28:53
2015-03-11 11:28:53
Desired output
2015-03-11 11:28:53
2015-03-11 11:28:55
2015-03-11 11:28:57
2015-03-11 11:28:59
2015-03-11 11:29:01
Any easy way to do this instead of placing the command
datetime.now().strftime('%Y-%m-%d %H:%M:%S')
multiple times in the code.
print datetime.now().strftime('%Y-%m-%d %H:%M:%S')in the loop itself?datetime.now()to measure the current time.