Recording the time into a text file
Question: I am trying to test how long my battery runs on my raspberry pi. All i need to
do is run a while loop until the battery dies. However, I need to record the
time that it starts and ends. I would like to save the data into a txt file.
My current code is as follows:

    
    
    import time
    
    file = open('time','w')
    
    x=1
    while x==1:
         from datetime import datetime
         now = datetime.now()
    
    file.write(now)
    file.close()
    

If I just print the results in python i will get a result, and the current
code makes a file called 'file' but there is nothing saved in the txt file.
Any and all help would be appreciated.

Thank you for your time


Answer: You have an infinite loop which just gets the current time over and over again
... This loop won't break (even if your battery dies ...).

At some point, you need to `break` the loop or the condition in the `while`
needs to become `False`. e.g.

    
    
    from datetime import datetime
    while x == 1:
        now = datetime.now()
        break
    

or

    
    
    from datetime import datetime
    while x == 1:
        now = datetime.now()
        x += 1
    

Generally speaking, you'll want to look in your system logs for when the
computer decided to start up and when it decided to shut down due to lack of
battery power ...

