Python Forum
Timestamp doesn't change with each loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Timestamp doesn't change with each loop
#1
  • 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)
Reply
#2
You have to call datetime.datetime.now() each time through the loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in timestamp Led_Zeppelin 3 3,191 Jun-15-2022, 08:28 PM
Last Post: deanhystad
  error in timestamp Led_Zeppelin 0 1,003 Jun-10-2022, 07:59 PM
Last Post: Led_Zeppelin
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 1,997 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Change the varaible in For Loop quest 2 1,709 Aug-13-2021, 03:35 PM
Last Post: deanhystad
  While loop doesn't exit KenHorse 3 1,998 Jun-20-2021, 11:05 PM
Last Post: deanhystad
  Change variable value during a while loop? penahuse 2 4,037 Nov-15-2020, 11:53 PM
Last Post: penahuse
  While loop doesn't end when False Kanashi 2 2,561 Nov-21-2019, 02:38 AM
Last Post: Kanashi
  For loop in my __init__ doesn't work as expected Jessy 2 2,353 Nov-18-2019, 10:07 AM
Last Post: buran
  (Python help) Change in logic not breaking 'while' loop? btcg2807 1 1,884 Sep-18-2019, 09:43 AM
Last Post: Larz60+
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 3,185 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020