For with python time

Asked

Viewed 1,463 times

2

Good morning Personal!
Can someone help me if it is possible to create a for in Python using timetables?

Example:

Set a start time for for and the other ending and the every 1 hour that for run again.

from datetime import datetime
now = datetime.now()

atual = now.hour
hr_final = 10
i = 0

for i in range(int(hr_final - atual)):
    print (atual)

3 answers

3

I don’t think it’s possible to use a date for the purpose you want.

calculations, operations and the like involving given are famous for obtaining a certain level of inherent 'complexity'.

I think you should broaden the scope of the question and ask yourself:

'It is possible to have a script that runs every 1 hour ?'

There are several ways to do this, I believe that one of the simplest (I will cite it as an example only for which you test) is using an appropriate library. The Twisted is an event-guided library that can be used to execute codes at intervals of time:

from twisted.internet import reactor, task
from os.path import getsize

FILE = '/var/log/syslog'

def monitor(lastsize = [-1])
    size = getsize(FILE)
    if size <> lastsize[0]:
        print 'O tamanho do arquivo agora é %d kilobytes (%d bytes)' % \
              (round(size / 1024.0), size)
        lastsize[0] = size

if __name__ == '__main__'
    print 'Checando a cada um minuto o tamanho do arquivo "%s".' % FILE
    print 'Atenção: esse programa NUNCA termina.'
    l = task.LoopingCall(monitor)
    l.start(1.0)
    reactor.run()

This code for example checks the file size every 1 second. However the use of a library as large and complex as this just to use a simple functionality NAY is recommended, it would be like using a rocket to kill a mosquito.

For a solution implemented by itself I strongly recommend that read this article posted in Python Brazil. There is demonstrated 3 examples of how to execute codes at predetermined intervals in a simple and well explained way.

I hope I’ve helped.

2


You can do this simply using the Sleep method of the time module

see an example of code that runs every 1 hour.

from time import sleep
from datetime import datetime

hora = 3600

while True:
    print datetime.now()
    sleep(hora)

In the code above we set the Sleep method to "wait" 3600 seconds than 1 hour

  • Thank you very much Rickadt, that’s exactly what I was looking for.

0

From what I understand you just want to increment the values, so use the object timedelta together with the datetime to increase or decrease variables with date and time:

from datetime import datetime, timedelta

inicio = datetime.now()

for i in range(0,10):
    print inicio + timedelta(hours=i)

And get as a result:

2018-12-24 11:23:44.750000
2018-12-24 12:23:44.750000
2018-12-24 13:23:44.750000
2018-12-24 14:23:44.750000
2018-12-24 15:23:44.750000
2018-12-24 16:23:44.750000
2018-12-24 17:23:44.750000
2018-12-24 18:23:44.750000
2018-12-24 19:23:44.750000
2018-12-24 20:23:44.750000

You can use "Seconds", "minutes", "days", "months" etc, alone or together to increment (or decrease) your object datetime.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.