Python Forum
Timestamp doesn't change with each loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Timestamp doesn't change with each loop (/thread-4531.html)



Timestamp doesn't change with each loop - lwhistler - Aug-23-2017

  • 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)


RE: Timestamp doesn't change with each loop - ichabod801 - Aug-23-2017

You have to call datetime.datetime.now() each time through the loop.


RE: Timestamp doesn't change with each loop - bowen73 - Aug-23-2017

(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)