L
L
Lerto2021-09-01 11:09:12
Python
Lerto, 2021-09-01 11:09:12

How to stop script in real time?

Good day. The script is required to stop between 11:45 and 15:00

in real time .

Wrote a script:

import time
if (time.strftime('now is %H') > 11/45 and < 15/00):
        break

I get an error:
AttributeError: 'int' object has no attribute 'strftime'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
HemulGM, 2021-09-01
@Lerto

First, look at the time.strftime method.
What does it take as arguments and what did you not specify there. And the time is not set like this (11/45). THIS IS A DIVISION. You just divided 11 by 45
And you can't write "and <". You can only compare two values. You need to write (X > Y) and (X < Z)

V
Vindicar, 2021-09-01
@Vindicar

AttributeError: 'int' object has no attribute 'strftime'
You have assigned time = somewhere, and you have lost the reference to the time module.
In general, yes, what you wrote is not syntactically correct python code!

import datetime
import time

now = datetime.datetime.now() #время в текущем временном поясе
stop_at = datetime.datetime(now.year, now.month, now.day, 11, 45, 00)

while datetime.datetime.now() < stop_at: #время можно сравнивать!
    print('tick...') #имитируем бурную деятельность
    time.sleep(1.0)
# ну или так
while True: #или какое другое условие
    if datetime.datetime.now() >= stop_at:
        break
    print('tick...')
    time.sleep(1.0)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question