简体   繁体   中英

Python if statement doesn't call function?

So I have following code:

 def timee():
    time.sleep(5)
    f = open("input.txt")
    lines = f.readlines()
    intimetxt = lines[0]
    now = datetime.now()
    currenttime = now.strftime("%H:%M")
    if currenttime == intimetxt:
        start()
    if not currenttime == lines[0]:
        timee() 
 timee()

It should read a sertain time from a.txt file and if the time is the current time it should call another function. My problem is that even if the time is the same, it won't call the function.

Hope somebody can help me out:)

The string you read from file using readlines() includes newline, try doing

intimetxt = lines[0].strip()

also, the condition may be simplified to:

if currenttime == intimetxt:
    start()
else:
    timee()

Even with that, the code still has problems, its recursively calling itself and opens the input file for every call. Try using while loop or calculating time to the start event and issuing right sleep()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM