Python Forum

Full Version: Timestamp doesn't change with each loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
  • How can I  get the timestamp to update after every loop?
  • The way it is now it just outputs the same time.
  • Since it is inside a loop I would expect it to update.


import datetime
import time


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

secondsDay=86400

while (secondsDay >= 1):
    degrees=str("{0:.2f}".format(secondsDay/240))
    timeStamp=now.strftime("%Y-%m-%d-%H:%M:%S")
    DataOut = timeStamp + "," + degrees + "\n"
    file = open(fileName,"a")
    file.write(DataOut)
    secondsDay=secondsDay-1
    print (DataOut)
    time.sleep(1)
You have to call datetime.datetime.now() each time through the loop.
(Aug-23-2017, 05:36 PM)ichabod801 Wrote: [ -> ]You have to call datetime.datetime.now() each time through the loop.

definitely this.... I came across it the other day too. I called the time towards the start of the script so it only loaded once and wasn't in the loop. calling the time within the loop pulls the time everytime it loops (i used it as a filename timestamp)